src/Pure/Syntax/mixfix.ML
author wenzelm
Wed Oct 24 17:31:58 2001 +0200 (2001-10-24)
changeset 11920 6833cadb4062
parent 11651 201b3f76c7b7
child 11978 a394d3e9c8bb
permissions -rw-r--r--
added string_of_mixfix;
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
clasohm@1181
    34
  val pure_appl_syntax: (string * string * mixfix) list
clasohm@1181
    35
  val pure_applC_syntax: (string * string * mixfix) list
oheimb@6027
    36
  val pure_sym_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@11920
    86
  | string_of_mixfix (Infixr p) = parens (spaces ["infixr", string_of_int p]);
wenzelm@11920
    87
wenzelm@11920
    88
wenzelm@384
    89
(* type / const names *)
wenzelm@384
    90
wenzelm@384
    91
fun strip ("'" :: c :: cs) = c :: strip cs
wenzelm@384
    92
  | strip ["'"] = []
wenzelm@384
    93
  | strip (c :: cs) = c :: strip cs
wenzelm@384
    94
  | strip [] = [];
wenzelm@384
    95
wenzelm@4697
    96
val strip_esc = implode o strip o Symbol.explode;
wenzelm@384
    97
wenzelm@11651
    98
fun type_name t (InfixName _) = t
wenzelm@11651
    99
  | type_name t (InfixlName _) = t
wenzelm@2199
   100
  | type_name t (InfixrName _) = t
wenzelm@11651
   101
  | type_name t (Infix _) = strip_esc t
wenzelm@2199
   102
  | type_name t (Infixl _) = strip_esc t
wenzelm@384
   103
  | type_name t (Infixr _) = strip_esc t
wenzelm@384
   104
  | type_name t _ = t;
wenzelm@384
   105
wenzelm@11651
   106
fun const_name c (InfixName _) = c
wenzelm@11651
   107
  | const_name c (InfixlName _) = c
wenzelm@2199
   108
  | const_name c (InfixrName _) = c
wenzelm@11651
   109
  | const_name c (Infix _) = "op " ^ strip_esc c
wenzelm@2199
   110
  | const_name c (Infixl _) = "op " ^ strip_esc c
wenzelm@2199
   111
  | const_name c (Infixr _) = "op " ^ strip_esc c
wenzelm@384
   112
  | const_name c _ = c;
wenzelm@384
   113
wenzelm@11651
   114
fun fix_mixfix c (Infix p) = (InfixName (c, p))
wenzelm@11651
   115
  | fix_mixfix c (Infixl p) = (InfixlName (c, p))
wenzelm@5056
   116
  | fix_mixfix c (Infixr p) = (InfixrName (c, p))
wenzelm@5056
   117
  | fix_mixfix _ mx = mx;
wenzelm@5056
   118
wenzelm@384
   119
wenzelm@4053
   120
(* mixfix_args *)
wenzelm@4053
   121
wenzelm@4053
   122
fun mixfix_args NoSyn = 0
wenzelm@5690
   123
  | mixfix_args (Mixfix (sy, _, _)) = SynExt.mfix_args sy
wenzelm@5690
   124
  | mixfix_args (Delimfix sy) = SynExt.mfix_args sy
wenzelm@4143
   125
  | mixfix_args (*infix or binder*) _ = 2;
wenzelm@4053
   126
wenzelm@4053
   127
wenzelm@384
   128
(* syn_ext_types *)
wenzelm@384
   129
clasohm@764
   130
fun syn_ext_types logtypes type_decls =
wenzelm@384
   131
  let
wenzelm@384
   132
    fun name_of (t, _, mx) = type_name t mx;
wenzelm@384
   133
wenzelm@2199
   134
    fun mk_infix sy t p1 p2 p3 =
wenzelm@5690
   135
      SynExt.Mfix ("(_ " ^ sy ^ "/ _)",
wenzelm@5690
   136
        [SynExt.typeT, SynExt.typeT] ---> SynExt.typeT, t, [p1, p2], p3);
wenzelm@384
   137
wenzelm@2199
   138
    fun mfix_of decl =
wenzelm@2199
   139
      let val t = name_of decl in
wenzelm@2199
   140
        (case decl of
wenzelm@2199
   141
          (_, _, NoSyn) => None
wenzelm@11651
   142
        | (_, 2, InfixName (sy, p)) => Some (mk_infix sy t (p + 1) (p + 1) p)
wenzelm@6419
   143
        | (_, 2, InfixlName (sy, p)) => Some (mk_infix sy t p (p + 1) p)
wenzelm@6419
   144
        | (_, 2, InfixrName (sy, p)) => Some (mk_infix sy t (p + 1) p p)
wenzelm@11651
   145
        | (sy, 2, Infix p) => Some (mk_infix sy t (p + 1) (p + 1) p)
wenzelm@6419
   146
        | (sy, 2, Infixl p) => Some (mk_infix sy t p (p + 1) p)
wenzelm@6419
   147
        | (sy, 2, Infixr p) => Some (mk_infix sy t (p + 1) p p)
wenzelm@2199
   148
        | _ => error ("Bad mixfix declaration for type " ^ quote t))
wenzelm@2199
   149
      end;
wenzelm@384
   150
wenzelm@384
   151
    val mfix = mapfilter mfix_of type_decls;
wenzelm@384
   152
    val xconsts = map name_of type_decls;
wenzelm@5690
   153
  in SynExt.syn_ext logtypes mfix xconsts ([], [], [], []) [] ([], []) end;
wenzelm@384
   154
wenzelm@384
   155
wenzelm@384
   156
(* syn_ext_consts *)
wenzelm@384
   157
clasohm@764
   158
fun syn_ext_consts logtypes const_decls =
wenzelm@384
   159
  let
wenzelm@384
   160
    fun name_of (c, _, mx) = const_name c mx;
wenzelm@384
   161
wenzelm@384
   162
    fun mk_infix sy ty c p1 p2 p3 =
wenzelm@5690
   163
      [SynExt.Mfix ("op " ^ sy, ty, c, [], SynExt.max_pri),
wenzelm@5690
   164
       SynExt.Mfix ("(_ " ^ sy ^ "/ _)", ty, c, [p1, p2], p3)];
wenzelm@384
   165
wenzelm@384
   166
    fun binder_typ _ (Type ("fun", [Type ("fun", [_, ty2]), ty3])) =
wenzelm@384
   167
          [Type ("idts", []), ty2] ---> ty3
wenzelm@384
   168
      | binder_typ c _ = error ("Bad type of binder " ^ quote c);
wenzelm@384
   169
wenzelm@2199
   170
    fun mfix_of decl =
wenzelm@2199
   171
      let val c = name_of decl in
wenzelm@2199
   172
        (case decl of
wenzelm@2199
   173
          (_, _, NoSyn) => []
wenzelm@5690
   174
        | (_, ty, Mixfix (sy, ps, p)) => [SynExt.Mfix (sy, ty, c, ps, p)]
wenzelm@5690
   175
        | (_, ty, Delimfix sy) => [SynExt.Mfix (sy, ty, c, [], SynExt.max_pri)]
wenzelm@11651
   176
        | (_, ty, InfixName (sy, p)) => mk_infix sy ty c (p + 1) (p + 1) p
wenzelm@2199
   177
        | (_, ty, InfixlName (sy, p)) => mk_infix sy ty c p (p + 1) p
wenzelm@2199
   178
        | (_, ty, InfixrName (sy, p)) => mk_infix sy ty c (p + 1) p p
wenzelm@11651
   179
        | (sy, ty, Infix p) => mk_infix sy ty c (p + 1) (p + 1) p
wenzelm@2199
   180
        | (sy, ty, Infixl p) => mk_infix sy ty c p (p + 1) p
wenzelm@2199
   181
        | (sy, ty, Infixr p) => mk_infix sy ty c (p + 1) p p
wenzelm@2199
   182
        | (_, ty, Binder (sy, p, q)) =>
wenzelm@5690
   183
            [SynExt.Mfix ("(3" ^ sy ^ "_./ _)", binder_typ c ty, sy, [0, p], q)])
wenzelm@2199
   184
    end;
wenzelm@384
   185
clasohm@865
   186
    fun binder (c, _, Binder (sy, _, _)) = Some (sy, c)
wenzelm@384
   187
      | binder _ = None;
wenzelm@384
   188
wenzelm@384
   189
    val mfix = flat (map mfix_of const_decls);
wenzelm@384
   190
    val xconsts = map name_of const_decls;
wenzelm@384
   191
    val binders = mapfilter binder const_decls;
wenzelm@5690
   192
    val binder_trs = map SynTrans.mk_binder_tr binders;
wenzelm@5690
   193
    val binder_trs' = map (apsnd SynExt.fix_tr' o SynTrans.mk_binder_tr' o swap) binders;
wenzelm@5690
   194
  in SynExt.syn_ext logtypes mfix xconsts ([], binder_trs, binder_trs', []) [] ([], []) end;
wenzelm@384
   195
wenzelm@384
   196
wenzelm@384
   197
wenzelm@384
   198
(** Pure syntax **)
wenzelm@384
   199
wenzelm@4920
   200
val pure_nonterms =
wenzelm@5690
   201
  (Lexicon.terminals @ [SynExt.logic, "type", "types", "sort", "classes",
wenzelm@5690
   202
    SynExt.args, SynExt.cargs, "pttrn", "pttrns", "idt", "idts", "aprop",
wenzelm@5690
   203
    "asms", SynExt.any, SynExt.sprop]);
wenzelm@384
   204
wenzelm@384
   205
val pure_syntax =
wenzelm@3690
   206
 [("_lambda",      "[pttrns, 'a] => logic",         Mixfix ("(3%_./ _)", [0, 3], 3)),
wenzelm@2696
   207
  ("_abs",         "'a",                            NoSyn),
wenzelm@5690
   208
  ("",             "'a => " ^ SynExt.args,          Delimfix "_"),
wenzelm@5690
   209
  ("_args",        "['a, " ^ SynExt.args ^ "] => " ^ SynExt.args, Delimfix "_,/ _"),
wenzelm@2696
   210
  ("",             "id => idt",                     Delimfix "_"),
wenzelm@2696
   211
  ("_idtyp",       "[id, type] => idt",             Mixfix ("_::_", [], 0)),
wenzelm@2696
   212
  ("",             "idt => idt",                    Delimfix "'(_')"),
wenzelm@3690
   213
  ("",             "idt => idts",                   Delimfix "_"),
wenzelm@3690
   214
  ("_idts",        "[idt, idts] => idts",           Mixfix ("_/ _", [1, 0], 0)),
wenzelm@2696
   215
  ("",             "idt => pttrn",                  Delimfix "_"),
wenzelm@3690
   216
  ("",             "pttrn => pttrns",               Delimfix "_"),
wenzelm@3690
   217
  ("_pttrns",      "[pttrn, pttrns] => pttrns",     Mixfix ("_/ _", [1, 0], 0)),
wenzelm@2696
   218
  ("",             "id => aprop",                   Delimfix "_"),
wenzelm@3829
   219
  ("",             "longid => aprop",               Delimfix "_"),
wenzelm@2696
   220
  ("",             "var => aprop",                  Delimfix "_"),
wenzelm@6929
   221
  ("_DDDOT",       "aprop",                         Delimfix "..."),
wenzelm@2696
   222
  ("_aprop",       "aprop => prop",                 Delimfix "PROP _"),
wenzelm@2696
   223
  ("",             "prop => asms",                  Delimfix "_"),
wenzelm@2696
   224
  ("_asms",        "[prop, asms] => asms",          Delimfix "_;/ _"),
wenzelm@2696
   225
  ("_bigimpl",     "[asms, prop] => prop",          Mixfix ("((3[| _ |])/ ==> _)", [0, 1], 1)),
wenzelm@2696
   226
  ("_ofclass",     "[type, logic] => prop",         Delimfix "(1OFCLASS/(1'(_,/ _')))"),
wenzelm@2696
   227
  ("_mk_ofclass",  "_",                             NoSyn),
wenzelm@4143
   228
  ("_TYPE",        "type => logic",                 Delimfix "(1TYPE/(1'(_')))"),
wenzelm@2696
   229
  ("_K",           "_",                             NoSyn),
wenzelm@2696
   230
  ("",             "id => logic",                   Delimfix "_"),
wenzelm@3829
   231
  ("",             "longid => logic",               Delimfix "_"),
wenzelm@5287
   232
  ("",             "var => logic",                  Delimfix "_"),
wenzelm@6759
   233
  ("_DDDOT",       "logic",                         Delimfix "...")];
wenzelm@384
   234
clasohm@1181
   235
val pure_appl_syntax =
wenzelm@5690
   236
 [("_appl", "[('b => 'a), args] => logic", Mixfix ("(1_/(1'(_')))", [SynExt.max_pri, 0], SynExt.max_pri)),
wenzelm@5690
   237
  ("_appl", "[('b => 'a), args] => aprop", Mixfix ("(1_/(1'(_')))", [SynExt.max_pri, 0], SynExt.max_pri))];
clasohm@1181
   238
clasohm@1181
   239
val pure_applC_syntax =
wenzelm@2696
   240
 [("",       "'a => cargs",                  Delimfix "_"),
wenzelm@5690
   241
  ("_cargs", "['a, cargs] => cargs",         Mixfix ("_/ _", [SynExt.max_pri, SynExt.max_pri], SynExt.max_pri)),
wenzelm@5690
   242
  ("_applC", "[('b => 'a), cargs] => logic", Mixfix ("(1_/ _)", [SynExt.max_pri, SynExt.max_pri], SynExt.max_pri - 1)),
wenzelm@5690
   243
  ("_applC", "[('b => 'a), cargs] => aprop", Mixfix ("(1_/ _)", [SynExt.max_pri, SynExt.max_pri], SynExt.max_pri - 1))];
wenzelm@2199
   244
wenzelm@2256
   245
val pure_sym_syntax =
wenzelm@2696
   246
 [("fun",      "[type, type] => type",  Mixfix ("(_/ \\<Rightarrow> _)", [1, 0], 0)),
wenzelm@2696
   247
  ("_bracket", "[types, type] => type", Mixfix ("([_]/ \\<Rightarrow> _)", [0, 0], 0)),
wenzelm@5690
   248
  ("_ofsort",  "[tid, sort] => type",   Mixfix ("_\\<Colon>_", [SynExt.max_pri, 0], SynExt.max_pri)),
wenzelm@2767
   249
  ("_constrain", "['a, type] => 'a",    Mixfix ("_\\<Colon>_", [4, 0], 3)),
wenzelm@2767
   250
  ("_idtyp",   "[id, type] => idt",     Mixfix ("_\\<Colon>_", [], 0)),
wenzelm@3690
   251
  ("_lambda",  "[pttrns, 'a] => logic", Mixfix ("(3\\<lambda>_./ _)", [0, 3], 3)),
wenzelm@2696
   252
  ("==>",      "[prop, prop] => prop",  InfixrName ("\\<Midarrow>\\<Rightarrow>", 1)),
wenzelm@2696
   253
  ("_bigimpl", "[asms, prop] => prop",  Mixfix ("((3\\<lbrakk>_\\<rbrakk>)/ \\<Midarrow>\\<Rightarrow> _)", [0, 1], 1)),
wenzelm@2696
   254
  ("==",       "['a::{}, 'a] => prop",  InfixrName ("\\<equiv>", 2)),
wenzelm@2696
   255
  ("!!",       "[idts, prop] => prop",  Mixfix ("(3\\<And>_./ _)", [0, 0], 0))];
wenzelm@2199
   256
oheimb@6027
   257
val pure_xsym_syntax =
oheimb@6027
   258
 [("==>",      "[prop, prop] => prop",  InfixrName ("\\<Longrightarrow>", 1)),
wenzelm@11098
   259
  ("=?=",      "['a::{}, 'a] => prop",  InfixrName ("\\<equiv>\\<^sup>?", 2)),
wenzelm@9734
   260
  ("_DDDOT",   "aprop",                 Delimfix "\\<dots>"),
wenzelm@9734
   261
  ("_bigimpl", "[asms, prop] => prop",  Mixfix ("((3\\<lbrakk>_\\<rbrakk>)/ \\<Longrightarrow> _)", [0, 1], 1)),
wenzelm@9734
   262
  ("_DDDOT",   "logic",                 Delimfix "\\<dots>")];
wenzelm@3829
   263
wenzelm@384
   264
end;