src/Pure/Syntax/mixfix.ML
author wenzelm
Fri Oct 28 22:27:51 2005 +0200 (2005-10-28)
changeset 18024 853e8219732a
parent 17787 b6e0d8323c0e
child 18341 d99396e96632
permissions -rw-r--r--
renamed Goal constant to prop;
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@12864
    26
  val literal: string -> mixfix
wenzelm@4823
    27
  val no_syn: 'a * 'b -> 'a * 'b * mixfix
wenzelm@11920
    28
  val string_of_mixfix: mixfix -> string
wenzelm@384
    29
  val type_name: string -> mixfix -> string
wenzelm@384
    30
  val const_name: string -> mixfix -> string
wenzelm@5056
    31
  val fix_mixfix: string -> mixfix -> mixfix
wenzelm@4823
    32
  val mixfix_args: mixfix -> int
wenzelm@4920
    33
  val pure_nonterms: string list
wenzelm@384
    34
  val pure_syntax: (string * string * mixfix) list
wenzelm@12149
    35
  val pure_syntax_output: (string * string * mixfix) list
clasohm@1181
    36
  val pure_appl_syntax: (string * string * mixfix) list
clasohm@1181
    37
  val pure_applC_syntax: (string * string * mixfix) list
oheimb@6027
    38
  val pure_xsym_syntax: (string * string * mixfix) list
wenzelm@2199
    39
end;
wenzelm@384
    40
wenzelm@384
    41
signature MIXFIX =
wenzelm@2199
    42
sig
wenzelm@384
    43
  include MIXFIX1
wenzelm@14903
    44
  val syn_ext_types: (string * int * mixfix) list -> SynExt.syn_ext
wenzelm@14903
    45
  val syn_ext_consts: (string -> bool) -> (string * typ * mixfix) list -> SynExt.syn_ext
wenzelm@2199
    46
end;
wenzelm@2199
    47
wenzelm@15751
    48
structure Mixfix: MIXFIX =
wenzelm@384
    49
struct
wenzelm@384
    50
wenzelm@2199
    51
wenzelm@384
    52
(** mixfix declarations **)
wenzelm@384
    53
wenzelm@384
    54
datatype mixfix =
wenzelm@384
    55
  NoSyn |
wenzelm@384
    56
  Mixfix of string * int list * int |
wenzelm@384
    57
  Delimfix of string |
wenzelm@11651
    58
  InfixName of string * int |
wenzelm@2199
    59
  InfixlName of string * int |
wenzelm@2199
    60
  InfixrName of string * int |
wenzelm@11651
    61
  Infix of int |
wenzelm@384
    62
  Infixl of int |
wenzelm@384
    63
  Infixr of int |
wenzelm@887
    64
  Binder of string * int * int;
wenzelm@384
    65
wenzelm@12864
    66
val literal = Delimfix o SynExt.escape_mfix;
wenzelm@12864
    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@12531
    91
(* syntax specifications *)
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@17284
   100
fun deprecated c = (warning ("Unnamed infix operator " ^ quote c ^ " -- deprecated"); c);
wenzelm@17284
   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@17284
   105
  | type_name t (Infix _) = deprecated (strip_esc t)
wenzelm@17284
   106
  | type_name t (Infixl _) = deprecated (strip_esc t)
wenzelm@17284
   107
  | type_name t (Infixr _) = deprecated (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@17284
   113
  | const_name c (Infix _) = "op " ^ deprecated (strip_esc c)
wenzelm@17284
   114
  | const_name c (Infixl _) = "op " ^ deprecated (strip_esc c)
wenzelm@17284
   115
  | const_name c (Infixr _) = "op " ^ deprecated (strip_esc c)
wenzelm@384
   116
  | const_name c _ = c;
wenzelm@384
   117
wenzelm@17284
   118
fun fix_mixfix c (Infix p) = (deprecated (strip_esc c); InfixName (c, p))
wenzelm@17284
   119
  | fix_mixfix c (Infixl p) = (deprecated (strip_esc c); InfixlName (c, p))
wenzelm@17284
   120
  | fix_mixfix c (Infixr p) = (deprecated (strip_esc c); 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
skalberg@15531
   145
          (_, _, NoSyn) => NONE
skalberg@15531
   146
        | (_, 2, InfixName (sy, p)) => SOME (mk_infix sy t (p + 1) (p + 1) p)
skalberg@15531
   147
        | (_, 2, InfixlName (sy, p)) => SOME (mk_infix sy t p (p + 1) p)
skalberg@15531
   148
        | (_, 2, InfixrName (sy, p)) => SOME (mk_infix sy t (p + 1) p p)
skalberg@15531
   149
        | (sy, 2, Infix p) => SOME (mk_infix sy t (p + 1) (p + 1) p)
skalberg@15531
   150
        | (sy, 2, Infixl p) => SOME (mk_infix sy t p (p + 1) p)
skalberg@15531
   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
skalberg@15570
   155
    val mfix = List.mapPartial 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@15751
   162
val binder_stamp = stamp ();
wenzelm@15751
   163
wenzelm@14903
   164
fun syn_ext_consts is_logtype 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@14903
   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
skalberg@15531
   192
    fun binder (c, _, Binder (sy, _, _)) = SOME (sy, c)
skalberg@15531
   193
      | binder _ = NONE;
wenzelm@384
   194
skalberg@15570
   195
    val mfix = List.concat (map mfix_of const_decls);
wenzelm@384
   196
    val xconsts = map name_of const_decls;
skalberg@15570
   197
    val binders = List.mapPartial binder const_decls;
wenzelm@16610
   198
    val binder_trs = binders |> map (SynExt.stamp_trfun binder_stamp o
wenzelm@16610
   199
        apsnd K o SynTrans.mk_binder_tr);
wenzelm@15751
   200
    val binder_trs' = binders |> map (SynExt.stamp_trfun binder_stamp o
wenzelm@16610
   201
        apsnd (K o SynTrans.non_typed_tr') o SynTrans.mk_binder_tr' o swap);
wenzelm@14903
   202
  in
wenzelm@14903
   203
    SynExt.syn_ext' true is_logtype
wenzelm@14903
   204
      mfix xconsts ([], binder_trs, binder_trs', []) [] ([], [])
wenzelm@14903
   205
  end;
wenzelm@384
   206
wenzelm@384
   207
wenzelm@384
   208
wenzelm@384
   209
(** Pure syntax **)
wenzelm@384
   210
wenzelm@4920
   211
val pure_nonterms =
wenzelm@5690
   212
  (Lexicon.terminals @ [SynExt.logic, "type", "types", "sort", "classes",
wenzelm@5690
   213
    SynExt.args, SynExt.cargs, "pttrn", "pttrns", "idt", "idts", "aprop",
wenzelm@12100
   214
    "asms", SynExt.any, SynExt.sprop, "num_const", "index", "struct"]);
wenzelm@384
   215
wenzelm@384
   216
val pure_syntax =
wenzelm@14665
   217
 [("_lambda",     "[pttrns, 'a] => logic",     Mixfix ("(3%_./ _)", [0, 3], 3)),
wenzelm@14665
   218
  ("_abs",        "'a",                        NoSyn),
wenzelm@14665
   219
  ("",            "'a => " ^ SynExt.args,      Delimfix "_"),
wenzelm@14665
   220
  ("_args",       "['a, " ^ SynExt.args ^ "] => " ^ SynExt.args, Delimfix "_,/ _"),
wenzelm@14665
   221
  ("",            "id => idt",                 Delimfix "_"),
wenzelm@17787
   222
  ("_idtdummy",   "idt",                       Delimfix "'_"),
wenzelm@14665
   223
  ("_idtyp",      "[id, type] => idt",         Mixfix ("_::_", [], 0)),
wenzelm@17787
   224
  ("_idtypdummy", "type => idt",               Mixfix ("'_()::_", [], 0)),
wenzelm@14665
   225
  ("",            "idt => idt",                Delimfix "'(_')"),
wenzelm@14665
   226
  ("",            "idt => idts",               Delimfix "_"),
wenzelm@14665
   227
  ("_idts",       "[idt, idts] => idts",       Mixfix ("_/ _", [1, 0], 0)),
wenzelm@14665
   228
  ("",            "idt => pttrn",              Delimfix "_"),
wenzelm@14665
   229
  ("",            "pttrn => pttrns",           Delimfix "_"),
wenzelm@14665
   230
  ("_pttrns",     "[pttrn, pttrns] => pttrns", Mixfix ("_/ _", [1, 0], 0)),
wenzelm@14665
   231
  ("",            "id => aprop",               Delimfix "_"),
wenzelm@14665
   232
  ("",            "longid => aprop",           Delimfix "_"),
wenzelm@14665
   233
  ("",            "var => aprop",              Delimfix "_"),
wenzelm@14665
   234
  ("_DDDOT",      "aprop",                     Delimfix "..."),
wenzelm@14665
   235
  ("_aprop",      "aprop => prop",             Delimfix "PROP _"),
schirmer@15421
   236
  ("_asm",        "prop => asms",              Delimfix "_"),
wenzelm@14665
   237
  ("_asms",       "[prop, asms] => asms",      Delimfix "_;/ _"),
wenzelm@14665
   238
  ("_bigimpl",    "[asms, prop] => prop",      Mixfix ("((3[| _ |])/ ==> _)", [0, 1], 1)),
wenzelm@14665
   239
  ("_ofclass",    "[type, logic] => prop",     Delimfix "(1OFCLASS/(1'(_,/ _')))"),
wenzelm@14665
   240
  ("_mk_ofclass", "_",                         NoSyn),
wenzelm@14665
   241
  ("_TYPE",       "type => logic",             Delimfix "(1TYPE/(1'(_')))"),
wenzelm@14665
   242
  ("",            "id => logic",               Delimfix "_"),
wenzelm@14665
   243
  ("",            "longid => logic",           Delimfix "_"),
wenzelm@14665
   244
  ("",            "var => logic",              Delimfix "_"),
wenzelm@14665
   245
  ("_DDDOT",      "logic",                     Delimfix "..."),
wenzelm@14665
   246
  ("_constify",   "num => num_const",          Delimfix "_"),
wenzelm@14697
   247
  ("_indexnum",   "num_const => index",        Delimfix "\\<^sub>_"),
wenzelm@14846
   248
  ("_index",      "logic => index",            Delimfix "(00\\<^bsub>_\\<^esub>)"),
wenzelm@14697
   249
  ("_indexdefault", "index",                   Delimfix ""),
wenzelm@14665
   250
  ("_indexvar",   "index",                     Delimfix "'\\<index>"),
wenzelm@14665
   251
  ("_struct",     "index => logic",            Mixfix ("\\<struct>_", [1000], 1000))];
wenzelm@384
   252
wenzelm@12149
   253
val pure_syntax_output =
wenzelm@18024
   254
 [("prop", "prop => prop", Mixfix ("_", [0], 0)),
wenzelm@12149
   255
  ("_meta_conjunction", "prop => prop => prop", InfixrName ("&&", 2))];
wenzelm@12149
   256
clasohm@1181
   257
val pure_appl_syntax =
wenzelm@5690
   258
 [("_appl", "[('b => 'a), args] => logic", Mixfix ("(1_/(1'(_')))", [SynExt.max_pri, 0], SynExt.max_pri)),
wenzelm@5690
   259
  ("_appl", "[('b => 'a), args] => aprop", Mixfix ("(1_/(1'(_')))", [SynExt.max_pri, 0], SynExt.max_pri))];
clasohm@1181
   260
clasohm@1181
   261
val pure_applC_syntax =
wenzelm@2696
   262
 [("",       "'a => cargs",                  Delimfix "_"),
wenzelm@5690
   263
  ("_cargs", "['a, cargs] => cargs",         Mixfix ("_/ _", [SynExt.max_pri, SynExt.max_pri], SynExt.max_pri)),
wenzelm@5690
   264
  ("_applC", "[('b => 'a), cargs] => logic", Mixfix ("(1_/ _)", [SynExt.max_pri, SynExt.max_pri], SynExt.max_pri - 1)),
wenzelm@5690
   265
  ("_applC", "[('b => 'a), cargs] => aprop", Mixfix ("(1_/ _)", [SynExt.max_pri, SynExt.max_pri], SynExt.max_pri - 1))];
wenzelm@2199
   266
wenzelm@12121
   267
val pure_xsym_syntax =
wenzelm@2696
   268
 [("fun",      "[type, type] => type",  Mixfix ("(_/ \\<Rightarrow> _)", [1, 0], 0)),
wenzelm@2696
   269
  ("_bracket", "[types, type] => type", Mixfix ("([_]/ \\<Rightarrow> _)", [0, 0], 0)),
wenzelm@5690
   270
  ("_ofsort",  "[tid, sort] => type",   Mixfix ("_\\<Colon>_", [SynExt.max_pri, 0], SynExt.max_pri)),
wenzelm@2767
   271
  ("_constrain", "['a, type] => 'a",    Mixfix ("_\\<Colon>_", [4, 0], 3)),
wenzelm@2767
   272
  ("_idtyp",   "[id, type] => idt",     Mixfix ("_\\<Colon>_", [], 0)),
wenzelm@17787
   273
  ("_idtypdummy", "type => idt",        Mixfix ("'_()\\<Colon>_", [], 0)),
wenzelm@3690
   274
  ("_lambda",  "[pttrns, 'a] => logic", Mixfix ("(3\\<lambda>_./ _)", [0, 3], 3)),
wenzelm@14855
   275
  ("==",       "['a, 'a] => prop",      InfixrName ("\\<equiv>", 2)),
wenzelm@12121
   276
  ("!!",       "[idts, prop] => prop",  Mixfix ("(3\\<And>_./ _)", [0, 0], 0)),
wenzelm@12121
   277
  ("==>",      "[prop, prop] => prop",  InfixrName ("\\<Longrightarrow>", 1)),
wenzelm@9734
   278
  ("_DDDOT",   "aprop",                 Delimfix "\\<dots>"),
nipkow@14363
   279
  ("_bigimpl", "[asms, prop] => prop",  Mixfix ("((1\\<lbrakk>_\\<rbrakk>)/ \\<Longrightarrow> _)", [0, 1], 1)),
wenzelm@9734
   280
  ("_DDDOT",   "logic",                 Delimfix "\\<dots>")];
wenzelm@3829
   281
wenzelm@384
   282
end;