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