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