src/Pure/Syntax/mixfix.ML
author wenzelm
Fri Dec 14 22:29:51 2001 +0100 (2001-12-14)
changeset 12512 ab14b29dfc6d
parent 12149 7cf8574102d5
child 12531 adc39b100e9a
permissions -rw-r--r--
removed special treatment of "_" in syntax (now covered by \<index> arg);
wenzelm@384
     1
(*  Title:      Pure/Syntax/mixfix.ML
wenzelm@384
     2
    ID:         $Id$
wenzelm@551
     3
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
wenzelm@384
     4
wenzelm@2199
     5
Mixfix declarations, infixes, binders. Also parts of the Pure syntax.
wenzelm@384
     6
*)
wenzelm@384
     7
wenzelm@384
     8
signature MIXFIX0 =
wenzelm@2199
     9
sig
wenzelm@384
    10
  datatype mixfix =
wenzelm@384
    11
    NoSyn |
wenzelm@384
    12
    Mixfix of string * int list * int |
wenzelm@384
    13
    Delimfix of string |
wenzelm@11651
    14
    InfixName of string * int |
wenzelm@2199
    15
    InfixlName of string * int |
wenzelm@2199
    16
    InfixrName of string * int |
wenzelm@11651
    17
    Infix of int |
wenzelm@384
    18
    Infixl of int |
wenzelm@384
    19
    Infixr of int |
clasohm@865
    20
    Binder of string * int * int
wenzelm@2199
    21
end;
wenzelm@384
    22
wenzelm@384
    23
signature MIXFIX1 =
wenzelm@2199
    24
sig
wenzelm@384
    25
  include MIXFIX0
wenzelm@4823
    26
  val no_syn: 'a * 'b -> 'a * 'b * mixfix
wenzelm@11920
    27
  val string_of_mixfix: mixfix -> string
wenzelm@384
    28
  val type_name: string -> mixfix -> string
wenzelm@384
    29
  val const_name: string -> mixfix -> string
wenzelm@5056
    30
  val fix_mixfix: string -> mixfix -> mixfix
wenzelm@4823
    31
  val mixfix_args: mixfix -> int
wenzelm@4920
    32
  val pure_nonterms: string list
wenzelm@384
    33
  val pure_syntax: (string * string * mixfix) list
wenzelm@12149
    34
  val pure_syntax_output: (string * string * mixfix) list
clasohm@1181
    35
  val pure_appl_syntax: (string * string * mixfix) list
clasohm@1181
    36
  val pure_applC_syntax: (string * string * mixfix) list
oheimb@6027
    37
  val pure_xsym_syntax: (string * string * mixfix) list
wenzelm@2199
    38
end;
wenzelm@384
    39
wenzelm@384
    40
signature MIXFIX =
wenzelm@2199
    41
sig
wenzelm@384
    42
  include MIXFIX1
wenzelm@2199
    43
  val syn_ext_types: string list -> (string * int * mixfix) list
wenzelm@2199
    44
    -> SynExt.syn_ext
wenzelm@2199
    45
  val syn_ext_consts: string list -> (string * typ * mixfix) list
wenzelm@2199
    46
    -> SynExt.syn_ext
wenzelm@2199
    47
end;
wenzelm@2199
    48
wenzelm@384
    49
paulson@1507
    50
structure Mixfix : MIXFIX =
wenzelm@384
    51
struct
wenzelm@384
    52
wenzelm@2199
    53
wenzelm@384
    54
(** mixfix declarations **)
wenzelm@384
    55
wenzelm@384
    56
datatype mixfix =
wenzelm@384
    57
  NoSyn |
wenzelm@384
    58
  Mixfix of string * int list * int |
wenzelm@384
    59
  Delimfix of string |
wenzelm@11651
    60
  InfixName of string * int |
wenzelm@2199
    61
  InfixlName of string * int |
wenzelm@2199
    62
  InfixrName of string * int |
wenzelm@11651
    63
  Infix of int |
wenzelm@384
    64
  Infixl of int |
wenzelm@384
    65
  Infixr of int |
wenzelm@887
    66
  Binder of string * int * int;
wenzelm@384
    67
wenzelm@4823
    68
fun no_syn (x, y) = (x, y, NoSyn);
wenzelm@4823
    69
wenzelm@384
    70
wenzelm@11920
    71
(* string_of_mixfix *)
wenzelm@11920
    72
wenzelm@11920
    73
val parens = enclose "(" ")";
wenzelm@11920
    74
val brackets = enclose "[" "]";
wenzelm@11920
    75
val spaces = space_implode " ";
wenzelm@11920
    76
wenzelm@11920
    77
fun string_of_mixfix NoSyn = ""
wenzelm@11920
    78
  | string_of_mixfix (Mixfix (sy, ps, p)) =
wenzelm@11920
    79
      parens (spaces [quote sy, brackets (commas (map string_of_int ps)), string_of_int p])
wenzelm@11920
    80
  | string_of_mixfix (Delimfix sy) = parens (quote sy)
wenzelm@11920
    81
  | string_of_mixfix (InfixName (sy, p)) = parens (spaces ["infix", quote sy, string_of_int p])
wenzelm@11920
    82
  | string_of_mixfix (InfixlName (sy, p)) = parens (spaces ["infixl", quote sy, string_of_int p])
wenzelm@11920
    83
  | string_of_mixfix (InfixrName (sy, p)) = parens (spaces ["infixl", quote sy, string_of_int p])
wenzelm@11920
    84
  | string_of_mixfix (Infix p) = parens (spaces ["infix", string_of_int p])
wenzelm@11920
    85
  | string_of_mixfix (Infixl p) = parens (spaces ["infixl", string_of_int p])
wenzelm@11978
    86
  | string_of_mixfix (Infixr p) = parens (spaces ["infixr", string_of_int p])
wenzelm@11978
    87
  | string_of_mixfix (Binder (sy, p1, p2)) =
wenzelm@11978
    88
      parens (spaces ["binder", brackets (string_of_int p1), string_of_int p2]);
wenzelm@11920
    89
wenzelm@11920
    90
wenzelm@384
    91
(* type / const names *)
wenzelm@384
    92
wenzelm@384
    93
fun strip ("'" :: c :: cs) = c :: strip cs
wenzelm@384
    94
  | strip ["'"] = []
wenzelm@384
    95
  | strip (c :: cs) = c :: strip cs
wenzelm@384
    96
  | strip [] = [];
wenzelm@384
    97
wenzelm@4697
    98
val strip_esc = implode o strip o Symbol.explode;
wenzelm@384
    99
wenzelm@11651
   100
fun type_name t (InfixName _) = t
wenzelm@11651
   101
  | type_name t (InfixlName _) = t
wenzelm@2199
   102
  | type_name t (InfixrName _) = t
wenzelm@11651
   103
  | type_name t (Infix _) = strip_esc t
wenzelm@2199
   104
  | type_name t (Infixl _) = strip_esc t
wenzelm@384
   105
  | type_name t (Infixr _) = strip_esc t
wenzelm@384
   106
  | type_name t _ = t;
wenzelm@384
   107
wenzelm@11651
   108
fun const_name c (InfixName _) = c
wenzelm@11651
   109
  | const_name c (InfixlName _) = c
wenzelm@2199
   110
  | const_name c (InfixrName _) = c
wenzelm@11651
   111
  | const_name c (Infix _) = "op " ^ strip_esc c
wenzelm@2199
   112
  | const_name c (Infixl _) = "op " ^ strip_esc c
wenzelm@2199
   113
  | const_name c (Infixr _) = "op " ^ strip_esc c
wenzelm@384
   114
  | const_name c _ = c;
wenzelm@384
   115
wenzelm@11651
   116
fun fix_mixfix c (Infix p) = (InfixName (c, p))
wenzelm@11651
   117
  | fix_mixfix c (Infixl p) = (InfixlName (c, p))
wenzelm@5056
   118
  | fix_mixfix c (Infixr p) = (InfixrName (c, p))
wenzelm@5056
   119
  | fix_mixfix _ mx = mx;
wenzelm@5056
   120
wenzelm@384
   121
wenzelm@4053
   122
(* mixfix_args *)
wenzelm@4053
   123
wenzelm@4053
   124
fun mixfix_args NoSyn = 0
wenzelm@5690
   125
  | mixfix_args (Mixfix (sy, _, _)) = SynExt.mfix_args sy
wenzelm@5690
   126
  | mixfix_args (Delimfix sy) = SynExt.mfix_args sy
wenzelm@4143
   127
  | mixfix_args (*infix or binder*) _ = 2;
wenzelm@4053
   128
wenzelm@4053
   129
wenzelm@384
   130
(* syn_ext_types *)
wenzelm@384
   131
clasohm@764
   132
fun syn_ext_types logtypes type_decls =
wenzelm@384
   133
  let
wenzelm@384
   134
    fun name_of (t, _, mx) = type_name t mx;
wenzelm@384
   135
wenzelm@2199
   136
    fun mk_infix sy t p1 p2 p3 =
wenzelm@5690
   137
      SynExt.Mfix ("(_ " ^ sy ^ "/ _)",
wenzelm@12512
   138
        [SynExt.typeT, SynExt.typeT] ---> SynExt.typeT, t, [p1, p2], p3);
wenzelm@384
   139
wenzelm@2199
   140
    fun mfix_of decl =
wenzelm@2199
   141
      let val t = name_of decl in
wenzelm@2199
   142
        (case decl of
wenzelm@2199
   143
          (_, _, NoSyn) => None
wenzelm@11651
   144
        | (_, 2, InfixName (sy, p)) => Some (mk_infix sy t (p + 1) (p + 1) p)
wenzelm@6419
   145
        | (_, 2, InfixlName (sy, p)) => Some (mk_infix sy t p (p + 1) p)
wenzelm@6419
   146
        | (_, 2, InfixrName (sy, p)) => Some (mk_infix sy t (p + 1) p p)
wenzelm@11651
   147
        | (sy, 2, Infix p) => Some (mk_infix sy t (p + 1) (p + 1) p)
wenzelm@6419
   148
        | (sy, 2, Infixl p) => Some (mk_infix sy t p (p + 1) p)
wenzelm@6419
   149
        | (sy, 2, Infixr p) => Some (mk_infix sy t (p + 1) p p)
wenzelm@2199
   150
        | _ => error ("Bad mixfix declaration for type " ^ quote t))
wenzelm@2199
   151
      end;
wenzelm@384
   152
wenzelm@384
   153
    val mfix = mapfilter mfix_of type_decls;
wenzelm@384
   154
    val xconsts = map name_of type_decls;
wenzelm@5690
   155
  in SynExt.syn_ext logtypes mfix xconsts ([], [], [], []) [] ([], []) end;
wenzelm@384
   156
wenzelm@384
   157
wenzelm@384
   158
(* syn_ext_consts *)
wenzelm@384
   159
clasohm@764
   160
fun syn_ext_consts logtypes const_decls =
wenzelm@384
   161
  let
wenzelm@384
   162
    fun name_of (c, _, mx) = const_name c mx;
wenzelm@384
   163
wenzelm@384
   164
    fun mk_infix sy ty c p1 p2 p3 =
wenzelm@5690
   165
      [SynExt.Mfix ("op " ^ sy, ty, c, [], SynExt.max_pri),
wenzelm@12512
   166
       SynExt.Mfix ("(_ " ^ sy ^ "/ _)", ty, c, [p1, p2], p3)];
wenzelm@384
   167
wenzelm@384
   168
    fun binder_typ _ (Type ("fun", [Type ("fun", [_, ty2]), ty3])) =
wenzelm@384
   169
          [Type ("idts", []), ty2] ---> ty3
wenzelm@384
   170
      | binder_typ c _ = error ("Bad type of binder " ^ quote c);
wenzelm@384
   171
wenzelm@2199
   172
    fun mfix_of decl =
wenzelm@2199
   173
      let val c = name_of decl in
wenzelm@2199
   174
        (case decl of
wenzelm@2199
   175
          (_, _, NoSyn) => []
wenzelm@5690
   176
        | (_, ty, Mixfix (sy, ps, p)) => [SynExt.Mfix (sy, ty, c, ps, p)]
wenzelm@5690
   177
        | (_, ty, Delimfix sy) => [SynExt.Mfix (sy, ty, c, [], SynExt.max_pri)]
wenzelm@11651
   178
        | (_, ty, InfixName (sy, p)) => mk_infix sy ty c (p + 1) (p + 1) p
wenzelm@2199
   179
        | (_, ty, InfixlName (sy, p)) => mk_infix sy ty c p (p + 1) p
wenzelm@2199
   180
        | (_, ty, InfixrName (sy, p)) => mk_infix sy ty c (p + 1) p p
wenzelm@11651
   181
        | (sy, ty, Infix p) => mk_infix sy ty c (p + 1) (p + 1) p
wenzelm@2199
   182
        | (sy, ty, Infixl p) => mk_infix sy ty c p (p + 1) p
wenzelm@2199
   183
        | (sy, ty, Infixr p) => mk_infix sy ty c (p + 1) p p
wenzelm@2199
   184
        | (_, ty, Binder (sy, p, q)) =>
wenzelm@5690
   185
            [SynExt.Mfix ("(3" ^ sy ^ "_./ _)", binder_typ c ty, sy, [0, p], q)])
wenzelm@2199
   186
    end;
wenzelm@384
   187
clasohm@865
   188
    fun binder (c, _, Binder (sy, _, _)) = Some (sy, c)
wenzelm@384
   189
      | binder _ = None;
wenzelm@384
   190
wenzelm@384
   191
    val mfix = flat (map mfix_of const_decls);
wenzelm@384
   192
    val xconsts = map name_of const_decls;
wenzelm@384
   193
    val binders = mapfilter binder const_decls;
wenzelm@5690
   194
    val binder_trs = map SynTrans.mk_binder_tr binders;
wenzelm@5690
   195
    val binder_trs' = map (apsnd SynExt.fix_tr' o SynTrans.mk_binder_tr' o swap) binders;
wenzelm@5690
   196
  in SynExt.syn_ext logtypes mfix xconsts ([], binder_trs, binder_trs', []) [] ([], []) end;
wenzelm@384
   197
wenzelm@384
   198
wenzelm@384
   199
wenzelm@384
   200
(** Pure syntax **)
wenzelm@384
   201
wenzelm@4920
   202
val pure_nonterms =
wenzelm@5690
   203
  (Lexicon.terminals @ [SynExt.logic, "type", "types", "sort", "classes",
wenzelm@5690
   204
    SynExt.args, SynExt.cargs, "pttrn", "pttrns", "idt", "idts", "aprop",
wenzelm@12100
   205
    "asms", SynExt.any, SynExt.sprop, "num_const", "index", "struct"]);
wenzelm@384
   206
wenzelm@384
   207
val pure_syntax =
wenzelm@3690
   208
 [("_lambda",      "[pttrns, 'a] => logic",         Mixfix ("(3%_./ _)", [0, 3], 3)),
wenzelm@2696
   209
  ("_abs",         "'a",                            NoSyn),
wenzelm@5690
   210
  ("",             "'a => " ^ SynExt.args,          Delimfix "_"),
wenzelm@5690
   211
  ("_args",        "['a, " ^ SynExt.args ^ "] => " ^ SynExt.args, Delimfix "_,/ _"),
wenzelm@2696
   212
  ("",             "id => idt",                     Delimfix "_"),
wenzelm@2696
   213
  ("_idtyp",       "[id, type] => idt",             Mixfix ("_::_", [], 0)),
wenzelm@2696
   214
  ("",             "idt => idt",                    Delimfix "'(_')"),
wenzelm@3690
   215
  ("",             "idt => idts",                   Delimfix "_"),
wenzelm@3690
   216
  ("_idts",        "[idt, idts] => idts",           Mixfix ("_/ _", [1, 0], 0)),
wenzelm@2696
   217
  ("",             "idt => pttrn",                  Delimfix "_"),
wenzelm@3690
   218
  ("",             "pttrn => pttrns",               Delimfix "_"),
wenzelm@3690
   219
  ("_pttrns",      "[pttrn, pttrns] => pttrns",     Mixfix ("_/ _", [1, 0], 0)),
wenzelm@2696
   220
  ("",             "id => aprop",                   Delimfix "_"),
wenzelm@3829
   221
  ("",             "longid => aprop",               Delimfix "_"),
wenzelm@2696
   222
  ("",             "var => aprop",                  Delimfix "_"),
wenzelm@6929
   223
  ("_DDDOT",       "aprop",                         Delimfix "..."),
wenzelm@2696
   224
  ("_aprop",       "aprop => prop",                 Delimfix "PROP _"),
wenzelm@2696
   225
  ("",             "prop => asms",                  Delimfix "_"),
wenzelm@2696
   226
  ("_asms",        "[prop, asms] => asms",          Delimfix "_;/ _"),
wenzelm@2696
   227
  ("_bigimpl",     "[asms, prop] => prop",          Mixfix ("((3[| _ |])/ ==> _)", [0, 1], 1)),
wenzelm@2696
   228
  ("_ofclass",     "[type, logic] => prop",         Delimfix "(1OFCLASS/(1'(_,/ _')))"),
wenzelm@2696
   229
  ("_mk_ofclass",  "_",                             NoSyn),
wenzelm@4143
   230
  ("_TYPE",        "type => logic",                 Delimfix "(1TYPE/(1'(_')))"),
wenzelm@2696
   231
  ("_K",           "_",                             NoSyn),
wenzelm@2696
   232
  ("",             "id => logic",                   Delimfix "_"),
wenzelm@3829
   233
  ("",             "longid => logic",               Delimfix "_"),
wenzelm@5287
   234
  ("",             "var => logic",                  Delimfix "_"),
wenzelm@12095
   235
  ("_DDDOT",       "logic",                         Delimfix "..."),
wenzelm@12100
   236
  ("_constify",    "num => num_const",              Delimfix "_"),
wenzelm@12100
   237
  ("_index",       "num_const => index",            Delimfix "\\<^sub>_"),
wenzelm@12512
   238
  ("_indexvar",    "index",                         Delimfix "'\\<index>"),
wenzelm@12100
   239
  ("_noindex",     "index",                         Delimfix ""),
wenzelm@12100
   240
  ("_struct",      "index => logic",                Delimfix "\\<struct>_")];
wenzelm@384
   241
wenzelm@12149
   242
val pure_syntax_output =
wenzelm@12149
   243
 [("Goal", "prop => prop", Mixfix ("_", [0], 0)),
wenzelm@12149
   244
  ("_meta_conjunction", "prop => prop => prop", InfixrName ("&&", 2))];
wenzelm@12149
   245
clasohm@1181
   246
val pure_appl_syntax =
wenzelm@5690
   247
 [("_appl", "[('b => 'a), args] => logic", Mixfix ("(1_/(1'(_')))", [SynExt.max_pri, 0], SynExt.max_pri)),
wenzelm@5690
   248
  ("_appl", "[('b => 'a), args] => aprop", Mixfix ("(1_/(1'(_')))", [SynExt.max_pri, 0], SynExt.max_pri))];
clasohm@1181
   249
clasohm@1181
   250
val pure_applC_syntax =
wenzelm@2696
   251
 [("",       "'a => cargs",                  Delimfix "_"),
wenzelm@5690
   252
  ("_cargs", "['a, cargs] => cargs",         Mixfix ("_/ _", [SynExt.max_pri, SynExt.max_pri], SynExt.max_pri)),
wenzelm@5690
   253
  ("_applC", "[('b => 'a), cargs] => logic", Mixfix ("(1_/ _)", [SynExt.max_pri, SynExt.max_pri], SynExt.max_pri - 1)),
wenzelm@5690
   254
  ("_applC", "[('b => 'a), cargs] => aprop", Mixfix ("(1_/ _)", [SynExt.max_pri, SynExt.max_pri], SynExt.max_pri - 1))];
wenzelm@2199
   255
wenzelm@12121
   256
val pure_xsym_syntax =
wenzelm@2696
   257
 [("fun",      "[type, type] => type",  Mixfix ("(_/ \\<Rightarrow> _)", [1, 0], 0)),
wenzelm@2696
   258
  ("_bracket", "[types, type] => type", Mixfix ("([_]/ \\<Rightarrow> _)", [0, 0], 0)),
wenzelm@5690
   259
  ("_ofsort",  "[tid, sort] => type",   Mixfix ("_\\<Colon>_", [SynExt.max_pri, 0], SynExt.max_pri)),
wenzelm@2767
   260
  ("_constrain", "['a, type] => 'a",    Mixfix ("_\\<Colon>_", [4, 0], 3)),
wenzelm@2767
   261
  ("_idtyp",   "[id, type] => idt",     Mixfix ("_\\<Colon>_", [], 0)),
wenzelm@3690
   262
  ("_lambda",  "[pttrns, 'a] => logic", Mixfix ("(3\\<lambda>_./ _)", [0, 3], 3)),
wenzelm@2696
   263
  ("==",       "['a::{}, 'a] => prop",  InfixrName ("\\<equiv>", 2)),
wenzelm@12121
   264
  ("!!",       "[idts, prop] => prop",  Mixfix ("(3\\<And>_./ _)", [0, 0], 0)),
wenzelm@12121
   265
  ("==>",      "[prop, prop] => prop",  InfixrName ("\\<Longrightarrow>", 1)),
wenzelm@11098
   266
  ("=?=",      "['a::{}, 'a] => prop",  InfixrName ("\\<equiv>\\<^sup>?", 2)),
wenzelm@9734
   267
  ("_DDDOT",   "aprop",                 Delimfix "\\<dots>"),
wenzelm@9734
   268
  ("_bigimpl", "[asms, prop] => prop",  Mixfix ("((3\\<lbrakk>_\\<rbrakk>)/ \\<Longrightarrow> _)", [0, 1], 1)),
wenzelm@9734
   269
  ("_DDDOT",   "logic",                 Delimfix "\\<dots>")];
wenzelm@3829
   270
wenzelm@384
   271
end;