src/Pure/Syntax/mixfix.ML
author wenzelm
Tue Oct 02 20:23:33 2001 +0200 (2001-10-02)
changeset 11651 201b3f76c7b7
parent 11098 a3299b153741
child 11920 6833cadb4062
permissions -rw-r--r--
support non-oriented infix;
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@384
    27
  val type_name: string -> mixfix -> string
wenzelm@384
    28
  val const_name: string -> mixfix -> string
wenzelm@5056
    29
  val fix_mixfix: string -> mixfix -> mixfix
wenzelm@4823
    30
  val mixfix_args: mixfix -> int
wenzelm@4920
    31
  val pure_nonterms: string list
wenzelm@384
    32
  val pure_syntax: (string * string * mixfix) list
clasohm@1181
    33
  val pure_appl_syntax: (string * string * mixfix) list
clasohm@1181
    34
  val pure_applC_syntax: (string * string * mixfix) list
oheimb@6027
    35
  val pure_sym_syntax:  (string * string * mixfix) list
oheimb@6027
    36
  val pure_xsym_syntax: (string * string * mixfix) list
wenzelm@2199
    37
end;
wenzelm@384
    38
wenzelm@384
    39
signature MIXFIX =
wenzelm@2199
    40
sig
wenzelm@384
    41
  include MIXFIX1
wenzelm@2199
    42
  val syn_ext_types: string list -> (string * int * mixfix) list
wenzelm@2199
    43
    -> SynExt.syn_ext
wenzelm@2199
    44
  val syn_ext_consts: string list -> (string * typ * mixfix) list
wenzelm@2199
    45
    -> SynExt.syn_ext
wenzelm@2199
    46
end;
wenzelm@2199
    47
wenzelm@384
    48
paulson@1507
    49
structure Mixfix : MIXFIX =
wenzelm@384
    50
struct
wenzelm@384
    51
wenzelm@2199
    52
wenzelm@384
    53
(** mixfix declarations **)
wenzelm@384
    54
wenzelm@384
    55
datatype mixfix =
wenzelm@384
    56
  NoSyn |
wenzelm@384
    57
  Mixfix of string * int list * int |
wenzelm@384
    58
  Delimfix of string |
wenzelm@11651
    59
  InfixName of string * int |
wenzelm@2199
    60
  InfixlName of string * int |
wenzelm@2199
    61
  InfixrName of string * int |
wenzelm@11651
    62
  Infix of int |
wenzelm@384
    63
  Infixl of int |
wenzelm@384
    64
  Infixr of int |
wenzelm@887
    65
  Binder of string * int * int;
wenzelm@384
    66
wenzelm@4823
    67
fun no_syn (x, y) = (x, y, NoSyn);
wenzelm@4823
    68
wenzelm@384
    69
wenzelm@384
    70
(* type / const names *)
wenzelm@384
    71
wenzelm@384
    72
fun strip ("'" :: c :: cs) = c :: strip cs
wenzelm@384
    73
  | strip ["'"] = []
wenzelm@384
    74
  | strip (c :: cs) = c :: strip cs
wenzelm@384
    75
  | strip [] = [];
wenzelm@384
    76
wenzelm@4697
    77
val strip_esc = implode o strip o Symbol.explode;
wenzelm@384
    78
wenzelm@11651
    79
fun type_name t (InfixName _) = t
wenzelm@11651
    80
  | type_name t (InfixlName _) = t
wenzelm@2199
    81
  | type_name t (InfixrName _) = t
wenzelm@11651
    82
  | type_name t (Infix _) = strip_esc t
wenzelm@2199
    83
  | type_name t (Infixl _) = strip_esc t
wenzelm@384
    84
  | type_name t (Infixr _) = strip_esc t
wenzelm@384
    85
  | type_name t _ = t;
wenzelm@384
    86
wenzelm@11651
    87
fun const_name c (InfixName _) = c
wenzelm@11651
    88
  | const_name c (InfixlName _) = c
wenzelm@2199
    89
  | const_name c (InfixrName _) = c
wenzelm@11651
    90
  | const_name c (Infix _) = "op " ^ strip_esc c
wenzelm@2199
    91
  | const_name c (Infixl _) = "op " ^ strip_esc c
wenzelm@2199
    92
  | const_name c (Infixr _) = "op " ^ strip_esc c
wenzelm@384
    93
  | const_name c _ = c;
wenzelm@384
    94
wenzelm@11651
    95
fun fix_mixfix c (Infix p) = (InfixName (c, p))
wenzelm@11651
    96
  | fix_mixfix c (Infixl p) = (InfixlName (c, p))
wenzelm@5056
    97
  | fix_mixfix c (Infixr p) = (InfixrName (c, p))
wenzelm@5056
    98
  | fix_mixfix _ mx = mx;
wenzelm@5056
    99
wenzelm@384
   100
wenzelm@4053
   101
(* mixfix_args *)
wenzelm@4053
   102
wenzelm@4053
   103
fun mixfix_args NoSyn = 0
wenzelm@5690
   104
  | mixfix_args (Mixfix (sy, _, _)) = SynExt.mfix_args sy
wenzelm@5690
   105
  | mixfix_args (Delimfix sy) = SynExt.mfix_args sy
wenzelm@4143
   106
  | mixfix_args (*infix or binder*) _ = 2;
wenzelm@4053
   107
wenzelm@4053
   108
wenzelm@384
   109
(* syn_ext_types *)
wenzelm@384
   110
clasohm@764
   111
fun syn_ext_types logtypes type_decls =
wenzelm@384
   112
  let
wenzelm@384
   113
    fun name_of (t, _, mx) = type_name t mx;
wenzelm@384
   114
wenzelm@2199
   115
    fun mk_infix sy t p1 p2 p3 =
wenzelm@5690
   116
      SynExt.Mfix ("(_ " ^ sy ^ "/ _)",
wenzelm@5690
   117
        [SynExt.typeT, SynExt.typeT] ---> SynExt.typeT, t, [p1, p2], p3);
wenzelm@384
   118
wenzelm@2199
   119
    fun mfix_of decl =
wenzelm@2199
   120
      let val t = name_of decl in
wenzelm@2199
   121
        (case decl of
wenzelm@2199
   122
          (_, _, NoSyn) => None
wenzelm@11651
   123
        | (_, 2, InfixName (sy, p)) => Some (mk_infix sy t (p + 1) (p + 1) p)
wenzelm@6419
   124
        | (_, 2, InfixlName (sy, p)) => Some (mk_infix sy t p (p + 1) p)
wenzelm@6419
   125
        | (_, 2, InfixrName (sy, p)) => Some (mk_infix sy t (p + 1) p p)
wenzelm@11651
   126
        | (sy, 2, Infix p) => Some (mk_infix sy t (p + 1) (p + 1) p)
wenzelm@6419
   127
        | (sy, 2, Infixl p) => Some (mk_infix sy t p (p + 1) p)
wenzelm@6419
   128
        | (sy, 2, Infixr p) => Some (mk_infix sy t (p + 1) p p)
wenzelm@2199
   129
        | _ => error ("Bad mixfix declaration for type " ^ quote t))
wenzelm@2199
   130
      end;
wenzelm@384
   131
wenzelm@384
   132
    val mfix = mapfilter mfix_of type_decls;
wenzelm@384
   133
    val xconsts = map name_of type_decls;
wenzelm@5690
   134
  in SynExt.syn_ext logtypes mfix xconsts ([], [], [], []) [] ([], []) end;
wenzelm@384
   135
wenzelm@384
   136
wenzelm@384
   137
(* syn_ext_consts *)
wenzelm@384
   138
clasohm@764
   139
fun syn_ext_consts logtypes const_decls =
wenzelm@384
   140
  let
wenzelm@384
   141
    fun name_of (c, _, mx) = const_name c mx;
wenzelm@384
   142
wenzelm@384
   143
    fun mk_infix sy ty c p1 p2 p3 =
wenzelm@5690
   144
      [SynExt.Mfix ("op " ^ sy, ty, c, [], SynExt.max_pri),
wenzelm@5690
   145
       SynExt.Mfix ("(_ " ^ sy ^ "/ _)", ty, c, [p1, p2], p3)];
wenzelm@384
   146
wenzelm@384
   147
    fun binder_typ _ (Type ("fun", [Type ("fun", [_, ty2]), ty3])) =
wenzelm@384
   148
          [Type ("idts", []), ty2] ---> ty3
wenzelm@384
   149
      | binder_typ c _ = error ("Bad type of binder " ^ quote c);
wenzelm@384
   150
wenzelm@2199
   151
    fun mfix_of decl =
wenzelm@2199
   152
      let val c = name_of decl in
wenzelm@2199
   153
        (case decl of
wenzelm@2199
   154
          (_, _, NoSyn) => []
wenzelm@5690
   155
        | (_, ty, Mixfix (sy, ps, p)) => [SynExt.Mfix (sy, ty, c, ps, p)]
wenzelm@5690
   156
        | (_, ty, Delimfix sy) => [SynExt.Mfix (sy, ty, c, [], SynExt.max_pri)]
wenzelm@11651
   157
        | (_, ty, InfixName (sy, p)) => mk_infix sy ty c (p + 1) (p + 1) p
wenzelm@2199
   158
        | (_, ty, InfixlName (sy, p)) => mk_infix sy ty c p (p + 1) p
wenzelm@2199
   159
        | (_, ty, InfixrName (sy, p)) => mk_infix sy ty c (p + 1) p p
wenzelm@11651
   160
        | (sy, ty, Infix p) => mk_infix sy ty c (p + 1) (p + 1) p
wenzelm@2199
   161
        | (sy, ty, Infixl p) => mk_infix sy ty c p (p + 1) p
wenzelm@2199
   162
        | (sy, ty, Infixr p) => mk_infix sy ty c (p + 1) p p
wenzelm@2199
   163
        | (_, ty, Binder (sy, p, q)) =>
wenzelm@5690
   164
            [SynExt.Mfix ("(3" ^ sy ^ "_./ _)", binder_typ c ty, sy, [0, p], q)])
wenzelm@2199
   165
    end;
wenzelm@384
   166
clasohm@865
   167
    fun binder (c, _, Binder (sy, _, _)) = Some (sy, c)
wenzelm@384
   168
      | binder _ = None;
wenzelm@384
   169
wenzelm@384
   170
    val mfix = flat (map mfix_of const_decls);
wenzelm@384
   171
    val xconsts = map name_of const_decls;
wenzelm@384
   172
    val binders = mapfilter binder const_decls;
wenzelm@5690
   173
    val binder_trs = map SynTrans.mk_binder_tr binders;
wenzelm@5690
   174
    val binder_trs' = map (apsnd SynExt.fix_tr' o SynTrans.mk_binder_tr' o swap) binders;
wenzelm@5690
   175
  in SynExt.syn_ext logtypes mfix xconsts ([], binder_trs, binder_trs', []) [] ([], []) end;
wenzelm@384
   176
wenzelm@384
   177
wenzelm@384
   178
wenzelm@384
   179
(** Pure syntax **)
wenzelm@384
   180
wenzelm@4920
   181
val pure_nonterms =
wenzelm@5690
   182
  (Lexicon.terminals @ [SynExt.logic, "type", "types", "sort", "classes",
wenzelm@5690
   183
    SynExt.args, SynExt.cargs, "pttrn", "pttrns", "idt", "idts", "aprop",
wenzelm@5690
   184
    "asms", SynExt.any, SynExt.sprop]);
wenzelm@384
   185
wenzelm@384
   186
val pure_syntax =
wenzelm@3690
   187
 [("_lambda",      "[pttrns, 'a] => logic",         Mixfix ("(3%_./ _)", [0, 3], 3)),
wenzelm@2696
   188
  ("_abs",         "'a",                            NoSyn),
wenzelm@5690
   189
  ("",             "'a => " ^ SynExt.args,          Delimfix "_"),
wenzelm@5690
   190
  ("_args",        "['a, " ^ SynExt.args ^ "] => " ^ SynExt.args, Delimfix "_,/ _"),
wenzelm@2696
   191
  ("",             "id => idt",                     Delimfix "_"),
wenzelm@2696
   192
  ("_idtyp",       "[id, type] => idt",             Mixfix ("_::_", [], 0)),
wenzelm@2696
   193
  ("",             "idt => idt",                    Delimfix "'(_')"),
wenzelm@3690
   194
  ("",             "idt => idts",                   Delimfix "_"),
wenzelm@3690
   195
  ("_idts",        "[idt, idts] => idts",           Mixfix ("_/ _", [1, 0], 0)),
wenzelm@2696
   196
  ("",             "idt => pttrn",                  Delimfix "_"),
wenzelm@3690
   197
  ("",             "pttrn => pttrns",               Delimfix "_"),
wenzelm@3690
   198
  ("_pttrns",      "[pttrn, pttrns] => pttrns",     Mixfix ("_/ _", [1, 0], 0)),
wenzelm@2696
   199
  ("",             "id => aprop",                   Delimfix "_"),
wenzelm@3829
   200
  ("",             "longid => aprop",               Delimfix "_"),
wenzelm@2696
   201
  ("",             "var => aprop",                  Delimfix "_"),
wenzelm@6929
   202
  ("_DDDOT",       "aprop",                         Delimfix "..."),
wenzelm@2696
   203
  ("_aprop",       "aprop => prop",                 Delimfix "PROP _"),
wenzelm@2696
   204
  ("",             "prop => asms",                  Delimfix "_"),
wenzelm@2696
   205
  ("_asms",        "[prop, asms] => asms",          Delimfix "_;/ _"),
wenzelm@2696
   206
  ("_bigimpl",     "[asms, prop] => prop",          Mixfix ("((3[| _ |])/ ==> _)", [0, 1], 1)),
wenzelm@2696
   207
  ("_ofclass",     "[type, logic] => prop",         Delimfix "(1OFCLASS/(1'(_,/ _')))"),
wenzelm@2696
   208
  ("_mk_ofclass",  "_",                             NoSyn),
wenzelm@4143
   209
  ("_TYPE",        "type => logic",                 Delimfix "(1TYPE/(1'(_')))"),
wenzelm@2696
   210
  ("_K",           "_",                             NoSyn),
wenzelm@2696
   211
  ("",             "id => logic",                   Delimfix "_"),
wenzelm@3829
   212
  ("",             "longid => logic",               Delimfix "_"),
wenzelm@5287
   213
  ("",             "var => logic",                  Delimfix "_"),
wenzelm@6759
   214
  ("_DDDOT",       "logic",                         Delimfix "...")];
wenzelm@384
   215
clasohm@1181
   216
val pure_appl_syntax =
wenzelm@5690
   217
 [("_appl", "[('b => 'a), args] => logic", Mixfix ("(1_/(1'(_')))", [SynExt.max_pri, 0], SynExt.max_pri)),
wenzelm@5690
   218
  ("_appl", "[('b => 'a), args] => aprop", Mixfix ("(1_/(1'(_')))", [SynExt.max_pri, 0], SynExt.max_pri))];
clasohm@1181
   219
clasohm@1181
   220
val pure_applC_syntax =
wenzelm@2696
   221
 [("",       "'a => cargs",                  Delimfix "_"),
wenzelm@5690
   222
  ("_cargs", "['a, cargs] => cargs",         Mixfix ("_/ _", [SynExt.max_pri, SynExt.max_pri], SynExt.max_pri)),
wenzelm@5690
   223
  ("_applC", "[('b => 'a), cargs] => logic", Mixfix ("(1_/ _)", [SynExt.max_pri, SynExt.max_pri], SynExt.max_pri - 1)),
wenzelm@5690
   224
  ("_applC", "[('b => 'a), cargs] => aprop", Mixfix ("(1_/ _)", [SynExt.max_pri, SynExt.max_pri], SynExt.max_pri - 1))];
wenzelm@2199
   225
wenzelm@2256
   226
val pure_sym_syntax =
wenzelm@2696
   227
 [("fun",      "[type, type] => type",  Mixfix ("(_/ \\<Rightarrow> _)", [1, 0], 0)),
wenzelm@2696
   228
  ("_bracket", "[types, type] => type", Mixfix ("([_]/ \\<Rightarrow> _)", [0, 0], 0)),
wenzelm@5690
   229
  ("_ofsort",  "[tid, sort] => type",   Mixfix ("_\\<Colon>_", [SynExt.max_pri, 0], SynExt.max_pri)),
wenzelm@2767
   230
  ("_constrain", "['a, type] => 'a",    Mixfix ("_\\<Colon>_", [4, 0], 3)),
wenzelm@2767
   231
  ("_idtyp",   "[id, type] => idt",     Mixfix ("_\\<Colon>_", [], 0)),
wenzelm@3690
   232
  ("_lambda",  "[pttrns, 'a] => logic", Mixfix ("(3\\<lambda>_./ _)", [0, 3], 3)),
wenzelm@2696
   233
  ("==>",      "[prop, prop] => prop",  InfixrName ("\\<Midarrow>\\<Rightarrow>", 1)),
wenzelm@2696
   234
  ("_bigimpl", "[asms, prop] => prop",  Mixfix ("((3\\<lbrakk>_\\<rbrakk>)/ \\<Midarrow>\\<Rightarrow> _)", [0, 1], 1)),
wenzelm@2696
   235
  ("==",       "['a::{}, 'a] => prop",  InfixrName ("\\<equiv>", 2)),
wenzelm@2696
   236
  ("!!",       "[idts, prop] => prop",  Mixfix ("(3\\<And>_./ _)", [0, 0], 0))];
wenzelm@2199
   237
oheimb@6027
   238
val pure_xsym_syntax =
oheimb@6027
   239
 [("==>",      "[prop, prop] => prop",  InfixrName ("\\<Longrightarrow>", 1)),
wenzelm@11098
   240
  ("=?=",      "['a::{}, 'a] => prop",  InfixrName ("\\<equiv>\\<^sup>?", 2)),
wenzelm@9734
   241
  ("_DDDOT",   "aprop",                 Delimfix "\\<dots>"),
wenzelm@9734
   242
  ("_bigimpl", "[asms, prop] => prop",  Mixfix ("((3\\<lbrakk>_\\<rbrakk>)/ \\<Longrightarrow> _)", [0, 1], 1)),
wenzelm@9734
   243
  ("_DDDOT",   "logic",                 Delimfix "\\<dots>")];
wenzelm@3829
   244
wenzelm@384
   245
end;