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