src/Pure/Syntax/mixfix.ML
author wenzelm
Wed Jun 09 18:54:26 2004 +0200 (2004-06-09)
changeset 14903 d264b8ad3eec
parent 14855 a58abaad4f45
child 14981 e73f8140af78
permissions -rw-r--r--
removed separate logtypes field of syntax;
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@14903
    45
  val syn_ext_types: (string * int * mixfix) list -> SynExt.syn_ext
wenzelm@14903
    46
  val syn_ext_consts: (string -> bool) -> (string * typ * mixfix) list -> 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@12864
    68
val literal = Delimfix o SynExt.escape_mfix;
wenzelm@12864
    69
wenzelm@4823
    70
fun no_syn (x, y) = (x, y, NoSyn);
wenzelm@4823
    71
wenzelm@384
    72
wenzelm@11920
    73
(* string_of_mixfix *)
wenzelm@11920
    74
wenzelm@11920
    75
val parens = enclose "(" ")";
wenzelm@11920
    76
val brackets = enclose "[" "]";
wenzelm@11920
    77
val spaces = space_implode " ";
wenzelm@11920
    78
wenzelm@11920
    79
fun string_of_mixfix NoSyn = ""
wenzelm@11920
    80
  | string_of_mixfix (Mixfix (sy, ps, p)) =
wenzelm@11920
    81
      parens (spaces [quote sy, brackets (commas (map string_of_int ps)), string_of_int p])
wenzelm@11920
    82
  | string_of_mixfix (Delimfix sy) = parens (quote sy)
wenzelm@11920
    83
  | string_of_mixfix (InfixName (sy, p)) = parens (spaces ["infix", quote sy, string_of_int p])
wenzelm@11920
    84
  | string_of_mixfix (InfixlName (sy, p)) = parens (spaces ["infixl", quote sy, string_of_int p])
wenzelm@11920
    85
  | string_of_mixfix (InfixrName (sy, p)) = parens (spaces ["infixl", quote sy, string_of_int p])
wenzelm@11920
    86
  | string_of_mixfix (Infix p) = parens (spaces ["infix", string_of_int p])
wenzelm@11920
    87
  | string_of_mixfix (Infixl p) = parens (spaces ["infixl", string_of_int p])
wenzelm@11978
    88
  | string_of_mixfix (Infixr p) = parens (spaces ["infixr", string_of_int p])
wenzelm@11978
    89
  | string_of_mixfix (Binder (sy, p1, p2)) =
wenzelm@11978
    90
      parens (spaces ["binder", brackets (string_of_int p1), string_of_int p2]);
wenzelm@11920
    91
wenzelm@11920
    92
wenzelm@12531
    93
(* syntax specifications *)
wenzelm@384
    94
wenzelm@384
    95
fun strip ("'" :: c :: cs) = c :: strip cs
wenzelm@384
    96
  | strip ["'"] = []
wenzelm@384
    97
  | strip (c :: cs) = c :: strip cs
wenzelm@384
    98
  | strip [] = [];
wenzelm@384
    99
wenzelm@4697
   100
val strip_esc = implode o strip o Symbol.explode;
wenzelm@384
   101
wenzelm@11651
   102
fun type_name t (InfixName _) = t
wenzelm@11651
   103
  | type_name t (InfixlName _) = t
wenzelm@2199
   104
  | type_name t (InfixrName _) = t
wenzelm@11651
   105
  | type_name t (Infix _) = strip_esc t
wenzelm@2199
   106
  | type_name t (Infixl _) = strip_esc t
wenzelm@384
   107
  | type_name t (Infixr _) = strip_esc t
wenzelm@384
   108
  | type_name t _ = t;
wenzelm@384
   109
wenzelm@11651
   110
fun const_name c (InfixName _) = c
wenzelm@11651
   111
  | const_name c (InfixlName _) = c
wenzelm@2199
   112
  | const_name c (InfixrName _) = c
wenzelm@11651
   113
  | const_name c (Infix _) = "op " ^ strip_esc c
wenzelm@2199
   114
  | const_name c (Infixl _) = "op " ^ strip_esc c
wenzelm@2199
   115
  | const_name c (Infixr _) = "op " ^ strip_esc c
wenzelm@384
   116
  | const_name c _ = c;
wenzelm@384
   117
wenzelm@11651
   118
fun fix_mixfix c (Infix p) = (InfixName (c, p))
wenzelm@11651
   119
  | fix_mixfix c (Infixl p) = (InfixlName (c, p))
wenzelm@5056
   120
  | fix_mixfix c (Infixr p) = (InfixrName (c, p))
wenzelm@5056
   121
  | fix_mixfix _ mx = mx;
wenzelm@5056
   122
wenzelm@4053
   123
fun mixfix_args NoSyn = 0
wenzelm@5690
   124
  | mixfix_args (Mixfix (sy, _, _)) = SynExt.mfix_args sy
wenzelm@5690
   125
  | mixfix_args (Delimfix sy) = SynExt.mfix_args sy
wenzelm@12531
   126
  | mixfix_args (InfixName (sy, _)) = 2 + SynExt.mfix_args sy
wenzelm@12531
   127
  | mixfix_args (InfixlName (sy, _)) = 2 + SynExt.mfix_args sy
wenzelm@12531
   128
  | mixfix_args (InfixrName (sy, _)) = 2 + SynExt.mfix_args sy
wenzelm@12531
   129
  | mixfix_args (*old infix or binder*) _ = 2;
wenzelm@4053
   130
wenzelm@4053
   131
wenzelm@384
   132
(* syn_ext_types *)
wenzelm@384
   133
wenzelm@14903
   134
fun syn_ext_types type_decls =
wenzelm@384
   135
  let
wenzelm@384
   136
    fun name_of (t, _, mx) = type_name t mx;
wenzelm@384
   137
wenzelm@2199
   138
    fun mk_infix sy t p1 p2 p3 =
wenzelm@5690
   139
      SynExt.Mfix ("(_ " ^ sy ^ "/ _)",
wenzelm@12512
   140
        [SynExt.typeT, SynExt.typeT] ---> SynExt.typeT, t, [p1, p2], p3);
wenzelm@384
   141
wenzelm@2199
   142
    fun mfix_of decl =
wenzelm@2199
   143
      let val t = name_of decl in
wenzelm@2199
   144
        (case decl of
wenzelm@2199
   145
          (_, _, NoSyn) => None
wenzelm@11651
   146
        | (_, 2, InfixName (sy, p)) => Some (mk_infix sy t (p + 1) (p + 1) p)
wenzelm@6419
   147
        | (_, 2, InfixlName (sy, p)) => Some (mk_infix sy t p (p + 1) p)
wenzelm@6419
   148
        | (_, 2, InfixrName (sy, p)) => Some (mk_infix sy t (p + 1) p p)
wenzelm@11651
   149
        | (sy, 2, Infix p) => Some (mk_infix sy t (p + 1) (p + 1) p)
wenzelm@6419
   150
        | (sy, 2, Infixl p) => Some (mk_infix sy t p (p + 1) p)
wenzelm@6419
   151
        | (sy, 2, Infixr p) => Some (mk_infix sy t (p + 1) p p)
wenzelm@14903
   152
        | _ => error ("Bad mixfix declaration for type: " ^ quote t))
wenzelm@2199
   153
      end;
wenzelm@384
   154
wenzelm@384
   155
    val mfix = mapfilter mfix_of type_decls;
wenzelm@384
   156
    val xconsts = map name_of type_decls;
wenzelm@14903
   157
  in SynExt.syn_ext mfix xconsts ([], [], [], []) [] ([], []) end;
wenzelm@384
   158
wenzelm@384
   159
wenzelm@384
   160
(* syn_ext_consts *)
wenzelm@384
   161
wenzelm@14903
   162
fun syn_ext_consts is_logtype const_decls =
wenzelm@384
   163
  let
wenzelm@384
   164
    fun name_of (c, _, mx) = const_name c mx;
wenzelm@384
   165
wenzelm@384
   166
    fun mk_infix sy ty c p1 p2 p3 =
wenzelm@5690
   167
      [SynExt.Mfix ("op " ^ sy, ty, c, [], SynExt.max_pri),
wenzelm@12512
   168
       SynExt.Mfix ("(_ " ^ sy ^ "/ _)", ty, c, [p1, p2], p3)];
wenzelm@384
   169
wenzelm@384
   170
    fun binder_typ _ (Type ("fun", [Type ("fun", [_, ty2]), ty3])) =
wenzelm@384
   171
          [Type ("idts", []), ty2] ---> ty3
wenzelm@14903
   172
      | binder_typ c _ = error ("Bad type of binder: " ^ quote c);
wenzelm@384
   173
wenzelm@2199
   174
    fun mfix_of decl =
wenzelm@2199
   175
      let val c = name_of decl in
wenzelm@2199
   176
        (case decl of
wenzelm@2199
   177
          (_, _, NoSyn) => []
wenzelm@5690
   178
        | (_, ty, Mixfix (sy, ps, p)) => [SynExt.Mfix (sy, ty, c, ps, p)]
wenzelm@5690
   179
        | (_, ty, Delimfix sy) => [SynExt.Mfix (sy, ty, c, [], SynExt.max_pri)]
wenzelm@11651
   180
        | (_, ty, InfixName (sy, p)) => mk_infix sy ty c (p + 1) (p + 1) p
wenzelm@2199
   181
        | (_, ty, InfixlName (sy, p)) => mk_infix sy ty c p (p + 1) p
wenzelm@2199
   182
        | (_, ty, InfixrName (sy, p)) => mk_infix sy ty c (p + 1) p p
wenzelm@11651
   183
        | (sy, ty, Infix p) => mk_infix sy ty c (p + 1) (p + 1) p
wenzelm@2199
   184
        | (sy, ty, Infixl p) => mk_infix sy ty c p (p + 1) p
wenzelm@2199
   185
        | (sy, ty, Infixr p) => mk_infix sy ty c (p + 1) p p
wenzelm@2199
   186
        | (_, ty, Binder (sy, p, q)) =>
wenzelm@5690
   187
            [SynExt.Mfix ("(3" ^ sy ^ "_./ _)", binder_typ c ty, sy, [0, p], q)])
wenzelm@2199
   188
    end;
wenzelm@384
   189
clasohm@865
   190
    fun binder (c, _, Binder (sy, _, _)) = Some (sy, c)
wenzelm@384
   191
      | binder _ = None;
wenzelm@384
   192
wenzelm@384
   193
    val mfix = flat (map mfix_of const_decls);
wenzelm@384
   194
    val xconsts = map name_of const_decls;
wenzelm@384
   195
    val binders = mapfilter binder const_decls;
wenzelm@5690
   196
    val binder_trs = map SynTrans.mk_binder_tr binders;
wenzelm@14647
   197
    val binder_trs' =
wenzelm@14647
   198
      map (apsnd SynTrans.non_typed_tr' o SynTrans.mk_binder_tr' o swap) binders;
wenzelm@14903
   199
  in
wenzelm@14903
   200
    SynExt.syn_ext' true is_logtype
wenzelm@14903
   201
      mfix xconsts ([], binder_trs, binder_trs', []) [] ([], [])
wenzelm@14903
   202
  end;
wenzelm@384
   203
wenzelm@384
   204
wenzelm@384
   205
wenzelm@384
   206
(** Pure syntax **)
wenzelm@384
   207
wenzelm@4920
   208
val pure_nonterms =
wenzelm@5690
   209
  (Lexicon.terminals @ [SynExt.logic, "type", "types", "sort", "classes",
wenzelm@5690
   210
    SynExt.args, SynExt.cargs, "pttrn", "pttrns", "idt", "idts", "aprop",
wenzelm@12100
   211
    "asms", SynExt.any, SynExt.sprop, "num_const", "index", "struct"]);
wenzelm@384
   212
wenzelm@384
   213
val pure_syntax =
wenzelm@14665
   214
 [("_lambda",     "[pttrns, 'a] => logic",     Mixfix ("(3%_./ _)", [0, 3], 3)),
wenzelm@14665
   215
  ("_abs",        "'a",                        NoSyn),
wenzelm@14665
   216
  ("",            "'a => " ^ SynExt.args,      Delimfix "_"),
wenzelm@14665
   217
  ("_args",       "['a, " ^ SynExt.args ^ "] => " ^ SynExt.args, Delimfix "_,/ _"),
wenzelm@14665
   218
  ("",            "id => idt",                 Delimfix "_"),
wenzelm@14665
   219
  ("_idtyp",      "[id, type] => idt",         Mixfix ("_::_", [], 0)),
wenzelm@14665
   220
  ("",            "idt => idt",                Delimfix "'(_')"),
wenzelm@14665
   221
  ("",            "idt => idts",               Delimfix "_"),
wenzelm@14665
   222
  ("_idts",       "[idt, idts] => idts",       Mixfix ("_/ _", [1, 0], 0)),
wenzelm@14665
   223
  ("",            "idt => pttrn",              Delimfix "_"),
wenzelm@14665
   224
  ("",            "pttrn => pttrns",           Delimfix "_"),
wenzelm@14665
   225
  ("_pttrns",     "[pttrn, pttrns] => pttrns", Mixfix ("_/ _", [1, 0], 0)),
wenzelm@14665
   226
  ("",            "id => aprop",               Delimfix "_"),
wenzelm@14665
   227
  ("",            "longid => aprop",           Delimfix "_"),
wenzelm@14665
   228
  ("",            "var => aprop",              Delimfix "_"),
wenzelm@14665
   229
  ("_DDDOT",      "aprop",                     Delimfix "..."),
wenzelm@14665
   230
  ("_aprop",      "aprop => prop",             Delimfix "PROP _"),
wenzelm@14665
   231
  ("",            "prop => asms",              Delimfix "_"),
wenzelm@14665
   232
  ("_asms",       "[prop, asms] => asms",      Delimfix "_;/ _"),
wenzelm@14665
   233
  ("_bigimpl",    "[asms, prop] => prop",      Mixfix ("((3[| _ |])/ ==> _)", [0, 1], 1)),
wenzelm@14665
   234
  ("_ofclass",    "[type, logic] => prop",     Delimfix "(1OFCLASS/(1'(_,/ _')))"),
wenzelm@14665
   235
  ("_mk_ofclass", "_",                         NoSyn),
wenzelm@14665
   236
  ("_TYPE",       "type => logic",             Delimfix "(1TYPE/(1'(_')))"),
wenzelm@14665
   237
  ("_K",          "_",                         NoSyn),
wenzelm@14665
   238
  ("",            "id => logic",               Delimfix "_"),
wenzelm@14665
   239
  ("",            "longid => logic",           Delimfix "_"),
wenzelm@14665
   240
  ("",            "var => logic",              Delimfix "_"),
wenzelm@14665
   241
  ("_DDDOT",      "logic",                     Delimfix "..."),
wenzelm@14665
   242
  ("_constify",   "num => num_const",          Delimfix "_"),
wenzelm@14697
   243
  ("_indexnum",   "num_const => index",        Delimfix "\\<^sub>_"),
wenzelm@14846
   244
  ("_index",      "logic => index",            Delimfix "(00\\<^bsub>_\\<^esub>)"),
wenzelm@14697
   245
  ("_indexdefault", "index",                   Delimfix ""),
wenzelm@14665
   246
  ("_indexvar",   "index",                     Delimfix "'\\<index>"),
wenzelm@14665
   247
  ("_struct",     "index => logic",            Mixfix ("\\<struct>_", [1000], 1000))];
wenzelm@384
   248
wenzelm@12149
   249
val pure_syntax_output =
wenzelm@12149
   250
 [("Goal", "prop => prop", Mixfix ("_", [0], 0)),
wenzelm@12149
   251
  ("_meta_conjunction", "prop => prop => prop", InfixrName ("&&", 2))];
wenzelm@12149
   252
clasohm@1181
   253
val pure_appl_syntax =
wenzelm@5690
   254
 [("_appl", "[('b => 'a), args] => logic", Mixfix ("(1_/(1'(_')))", [SynExt.max_pri, 0], SynExt.max_pri)),
wenzelm@5690
   255
  ("_appl", "[('b => 'a), args] => aprop", Mixfix ("(1_/(1'(_')))", [SynExt.max_pri, 0], SynExt.max_pri))];
clasohm@1181
   256
clasohm@1181
   257
val pure_applC_syntax =
wenzelm@2696
   258
 [("",       "'a => cargs",                  Delimfix "_"),
wenzelm@5690
   259
  ("_cargs", "['a, cargs] => cargs",         Mixfix ("_/ _", [SynExt.max_pri, SynExt.max_pri], SynExt.max_pri)),
wenzelm@5690
   260
  ("_applC", "[('b => 'a), cargs] => logic", Mixfix ("(1_/ _)", [SynExt.max_pri, SynExt.max_pri], SynExt.max_pri - 1)),
wenzelm@5690
   261
  ("_applC", "[('b => 'a), cargs] => aprop", Mixfix ("(1_/ _)", [SynExt.max_pri, SynExt.max_pri], SynExt.max_pri - 1))];
wenzelm@2199
   262
wenzelm@12121
   263
val pure_xsym_syntax =
wenzelm@2696
   264
 [("fun",      "[type, type] => type",  Mixfix ("(_/ \\<Rightarrow> _)", [1, 0], 0)),
wenzelm@2696
   265
  ("_bracket", "[types, type] => type", Mixfix ("([_]/ \\<Rightarrow> _)", [0, 0], 0)),
wenzelm@5690
   266
  ("_ofsort",  "[tid, sort] => type",   Mixfix ("_\\<Colon>_", [SynExt.max_pri, 0], SynExt.max_pri)),
wenzelm@2767
   267
  ("_constrain", "['a, type] => 'a",    Mixfix ("_\\<Colon>_", [4, 0], 3)),
wenzelm@2767
   268
  ("_idtyp",   "[id, type] => idt",     Mixfix ("_\\<Colon>_", [], 0)),
wenzelm@3690
   269
  ("_lambda",  "[pttrns, 'a] => logic", Mixfix ("(3\\<lambda>_./ _)", [0, 3], 3)),
wenzelm@14855
   270
  ("==",       "['a, 'a] => prop",      InfixrName ("\\<equiv>", 2)),
wenzelm@12121
   271
  ("!!",       "[idts, prop] => prop",  Mixfix ("(3\\<And>_./ _)", [0, 0], 0)),
wenzelm@12121
   272
  ("==>",      "[prop, prop] => prop",  InfixrName ("\\<Longrightarrow>", 1)),
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;