src/Pure/Syntax/mixfix.ML
author wenzelm
Thu May 19 16:16:36 1994 +0200 (1994-05-19)
changeset 384 a8d204d8071d
child 472 bbaa2a02bd82
permissions -rw-r--r--
support for new style mixfix annotations;
part of the pure syntax as mixfix (supports axclasses);
wenzelm@384
     1
(*  Title:      Pure/Syntax/mixfix.ML
wenzelm@384
     2
    ID:         $Id$
wenzelm@384
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@384
     4
wenzelm@384
     5
Mixfix declarations, infixes, binders. Part of the Pure syntax.
wenzelm@384
     6
wenzelm@384
     7
TODO:
wenzelm@384
     8
  note: needs mk_binder_tr/tr'
wenzelm@384
     9
  Mixfix.mixfix -> mixfix
wenzelm@384
    10
  remove ....Mixfix
wenzelm@384
    11
*)
wenzelm@384
    12
wenzelm@384
    13
signature MIXFIX0 =
wenzelm@384
    14
sig
wenzelm@384
    15
  datatype mixfix =
wenzelm@384
    16
    NoSyn |
wenzelm@384
    17
    Mixfix of string * int list * int |
wenzelm@384
    18
    Delimfix of string |
wenzelm@384
    19
    Infixl of int |
wenzelm@384
    20
    Infixr of int |
wenzelm@384
    21
    Binder of string * int
wenzelm@384
    22
  val max_pri: int
wenzelm@384
    23
end;
wenzelm@384
    24
wenzelm@384
    25
signature MIXFIX1 =
wenzelm@384
    26
sig
wenzelm@384
    27
  include MIXFIX0
wenzelm@384
    28
  val type_name: string -> mixfix -> string
wenzelm@384
    29
  val const_name: string -> mixfix -> string
wenzelm@384
    30
  val pure_types: (string * int * mixfix) list
wenzelm@384
    31
  val pure_syntax: (string * string * mixfix) list
wenzelm@384
    32
end;
wenzelm@384
    33
wenzelm@384
    34
signature MIXFIX =
wenzelm@384
    35
sig
wenzelm@384
    36
  include MIXFIX1
wenzelm@384
    37
  structure SynExt: SYN_EXT
wenzelm@384
    38
  local open SynExt in
wenzelm@384
    39
    val syn_ext_types: string list -> (string * int * mixfix) list -> syn_ext
wenzelm@384
    40
    val syn_ext_consts: string list -> (string * typ * mixfix) list -> syn_ext
wenzelm@384
    41
  end
wenzelm@384
    42
end;
wenzelm@384
    43
wenzelm@384
    44
functor MixfixFun(structure Lexicon: LEXICON and SynExt: SYN_EXT
wenzelm@384
    45
  and SExtension: SEXTENSION): MIXFIX =
wenzelm@384
    46
struct
wenzelm@384
    47
wenzelm@384
    48
structure SynExt = SynExt;
wenzelm@384
    49
open Lexicon SynExt SynExt.Ast SExtension;
wenzelm@384
    50
wenzelm@384
    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@384
    58
  Infixl of int |
wenzelm@384
    59
  Infixr of int |
wenzelm@384
    60
  Binder of string * 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@384
    72
wenzelm@384
    73
fun type_name t (Infixl _) = strip_esc t
wenzelm@384
    74
  | type_name t (Infixr _) = strip_esc t
wenzelm@384
    75
  | type_name t _ = t;
wenzelm@384
    76
wenzelm@384
    77
fun infix_name c = "op " ^ strip_esc c;
wenzelm@384
    78
wenzelm@384
    79
fun const_name c (Infixl _) = infix_name c
wenzelm@384
    80
  | const_name c (Infixr _) = infix_name c
wenzelm@384
    81
  | const_name c _ = c;
wenzelm@384
    82
wenzelm@384
    83
wenzelm@384
    84
(* syn_ext_types *)
wenzelm@384
    85
wenzelm@384
    86
fun syn_ext_types all_roots type_decls =
wenzelm@384
    87
  let
wenzelm@384
    88
    fun name_of (t, _, mx) = type_name t mx;
wenzelm@384
    89
wenzelm@384
    90
    fun mk_infix t p1 p2 p3 =
wenzelm@384
    91
      Mfix ("(_ " ^ t ^ "/ _)", [typeT, typeT] ---> typeT,
wenzelm@384
    92
        strip_esc t, [p1, p2], p3);
wenzelm@384
    93
wenzelm@384
    94
    fun mfix_of (_, _, NoSyn) = None
wenzelm@384
    95
      | mfix_of (t, 2, Infixl p) = Some (mk_infix t p (p + 1) p)
wenzelm@384
    96
      | mfix_of (t, 2, Infixr p) = Some (mk_infix t (p + 1) p p)
wenzelm@384
    97
      | mfix_of decl = error ("Bad mixfix declaration for type " ^
wenzelm@384
    98
          quote (name_of decl));
wenzelm@384
    99
wenzelm@384
   100
    val mfix = mapfilter mfix_of type_decls;
wenzelm@384
   101
    val xconsts = map name_of type_decls;
wenzelm@384
   102
  in
wenzelm@384
   103
    syn_ext all_roots [] mfix xconsts ([], [], [], []) ([], [])
wenzelm@384
   104
  end;
wenzelm@384
   105
wenzelm@384
   106
wenzelm@384
   107
(* syn_ext_consts *)
wenzelm@384
   108
wenzelm@384
   109
fun syn_ext_consts all_roots const_decls =
wenzelm@384
   110
  let
wenzelm@384
   111
    fun name_of (c, _, mx) = const_name c mx;
wenzelm@384
   112
wenzelm@384
   113
    fun mk_infix sy ty c p1 p2 p3 =
wenzelm@384
   114
      [Mfix ("(_ " ^ sy ^ "/ _)", ty, c, [p1, p2], p3),
wenzelm@384
   115
       Mfix ("op " ^ sy, ty, c, [], max_pri)];
wenzelm@384
   116
wenzelm@384
   117
    fun binder_typ _ (Type ("fun", [Type ("fun", [_, ty2]), ty3])) =
wenzelm@384
   118
          [Type ("idts", []), ty2] ---> ty3
wenzelm@384
   119
      | binder_typ c _ = error ("Bad type of binder " ^ quote c);
wenzelm@384
   120
wenzelm@384
   121
    fun mfix_of (_, _, NoSyn) = []
wenzelm@384
   122
      | mfix_of (c, ty, Mixfix (sy, ps, p)) = [Mfix (sy, ty, c, ps, p)]
wenzelm@384
   123
      | mfix_of (c, ty, Delimfix sy) = [Mfix (sy, ty, c, [], max_pri)]
wenzelm@384
   124
      | mfix_of (sy, ty, Infixl p) = mk_infix sy ty (infix_name sy) p (p + 1) p
wenzelm@384
   125
      | mfix_of (sy, ty, Infixr p) = mk_infix sy ty (infix_name sy) (p + 1) p p
wenzelm@384
   126
      | mfix_of (c, ty, Binder (sy, p)) =
wenzelm@384
   127
          [Mfix ("(3" ^ sy ^ "_./ _)", binder_typ c ty, sy, [], p)];
wenzelm@384
   128
wenzelm@384
   129
    fun binder (c, _, Binder (sy, _)) = Some (sy, c)
wenzelm@384
   130
      | binder _ = None;
wenzelm@384
   131
wenzelm@384
   132
    val mfix = flat (map mfix_of const_decls);
wenzelm@384
   133
    val xconsts = map name_of const_decls;
wenzelm@384
   134
    val binders = mapfilter binder const_decls;
wenzelm@384
   135
    val binder_trs = map mk_binder_tr binders;
wenzelm@384
   136
    val binder_trs' = map (mk_binder_tr' o swap) binders;
wenzelm@384
   137
  in
wenzelm@384
   138
    syn_ext all_roots [] mfix xconsts ([], binder_trs, binder_trs', []) ([], [])
wenzelm@384
   139
  end;
wenzelm@384
   140
wenzelm@384
   141
wenzelm@384
   142
wenzelm@384
   143
(** Pure syntax **)
wenzelm@384
   144
wenzelm@384
   145
val pure_types =
wenzelm@384
   146
  map (fn t => (t, 0, NoSyn))
wenzelm@384
   147
    (terminals @ [logic, "type", "types", "sort", "classes", args, "idt",
wenzelm@384
   148
      "idts", "aprop", "asms"]);
wenzelm@384
   149
wenzelm@384
   150
val pure_syntax =
wenzelm@384
   151
 [("_lambda",   "[idts, 'a] => ('b => 'a)",       Mixfix ("(3%_./ _)", [], 0)),
wenzelm@384
   152
  ("",          "'a => " ^ args,                  Delimfix "_"),
wenzelm@384
   153
  ("_args",     "['a, " ^ args ^ "] => " ^ args,  Delimfix "_,/ _"),
wenzelm@384
   154
  ("",          "id => idt",                      Delimfix "_"),
wenzelm@384
   155
  ("_idtyp",    "[id, type] => idt",              Mixfix ("_::_", [], 0)),
wenzelm@384
   156
  ("",          "idt => idt",                     Delimfix "'(_')"),
wenzelm@384
   157
  ("",          "idt => idts",                    Delimfix "_"),
wenzelm@384
   158
  ("_idts",     "[idt, idts] => idts",            Mixfix ("_/ _", [1, 0], 0)),
wenzelm@384
   159
  ("",          "id => aprop",                    Delimfix "_"),
wenzelm@384
   160
  ("",          "var => aprop",                   Delimfix "_"),
wenzelm@384
   161
  (applC,       "[('b => 'a), " ^ args ^ "] => aprop",
wenzelm@384
   162
    Mixfix ("(1_/(1'(_')))", [max_pri, 0], max_pri)),
wenzelm@384
   163
  ("_aprop",    "aprop => prop",                  Delimfix "PROP _"),
wenzelm@384
   164
  ("",          "prop => asms",                   Delimfix "_"),
wenzelm@384
   165
  ("_asms",     "[prop, asms] => asms",           Delimfix "_;/ _"),
wenzelm@384
   166
  ("_bigimpl",  "[asms, prop] => prop",           Mixfix ("((3[| _ |]) ==>/ _)", [0, 1], 1)),
wenzelm@384
   167
  ("_insort",   "[type, 'a] => prop",             Delimfix "(2'(| _ :/ _ |'))"),
wenzelm@384
   168
  ("_K",        "'a",                             NoSyn),
wenzelm@384
   169
  ("_explode",  "'a",                             NoSyn),
wenzelm@384
   170
  ("_implode",  "'a",                             NoSyn)];
wenzelm@384
   171
wenzelm@384
   172
wenzelm@384
   173
end;
wenzelm@384
   174