src/Pure/Syntax/mixfix.ML
author wenzelm
Fri Jan 27 13:33:52 1995 +0100 (1995-01-27)
changeset 887 6a054d83acb2
parent 865 b38c67991122
child 921 6bee3815c0bf
permissions -rw-r--r--
*** empty log message ***
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@384
     5
Mixfix declarations, infixes, binders. Part of the Pure syntax.
wenzelm@384
     6
*)
wenzelm@384
     7
wenzelm@384
     8
signature MIXFIX0 =
wenzelm@384
     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@384
    14
    Infixl of int |
wenzelm@384
    15
    Infixr of int |
clasohm@865
    16
    Binder of string * int * int
wenzelm@384
    17
  val max_pri: int
wenzelm@384
    18
end;
wenzelm@384
    19
wenzelm@384
    20
signature MIXFIX1 =
wenzelm@384
    21
sig
wenzelm@384
    22
  include MIXFIX0
wenzelm@384
    23
  val type_name: string -> mixfix -> string
wenzelm@384
    24
  val const_name: string -> mixfix -> string
wenzelm@384
    25
  val pure_types: (string * int * mixfix) list
wenzelm@384
    26
  val pure_syntax: (string * string * mixfix) list
wenzelm@384
    27
end;
wenzelm@384
    28
wenzelm@384
    29
signature MIXFIX =
wenzelm@384
    30
sig
wenzelm@384
    31
  include MIXFIX1
wenzelm@384
    32
  structure SynExt: SYN_EXT
wenzelm@384
    33
  local open SynExt in
wenzelm@384
    34
    val syn_ext_types: string list -> (string * int * mixfix) list -> syn_ext
wenzelm@384
    35
    val syn_ext_consts: string list -> (string * typ * mixfix) list -> syn_ext
wenzelm@384
    36
  end
wenzelm@384
    37
end;
wenzelm@384
    38
wenzelm@384
    39
functor MixfixFun(structure Lexicon: LEXICON and SynExt: SYN_EXT
wenzelm@551
    40
  and SynTrans: SYN_TRANS): MIXFIX =
wenzelm@384
    41
struct
wenzelm@384
    42
wenzelm@384
    43
structure SynExt = SynExt;
wenzelm@551
    44
open Lexicon SynExt SynExt.Ast SynTrans;
wenzelm@384
    45
wenzelm@384
    46
wenzelm@384
    47
(** mixfix declarations **)
wenzelm@384
    48
wenzelm@384
    49
datatype mixfix =
wenzelm@384
    50
  NoSyn |
wenzelm@384
    51
  Mixfix of string * int list * int |
wenzelm@384
    52
  Delimfix of string |
wenzelm@384
    53
  Infixl of int |
wenzelm@384
    54
  Infixr of int |
wenzelm@887
    55
  Binder of string * int * int;
wenzelm@384
    56
wenzelm@384
    57
wenzelm@384
    58
(* type / const names *)
wenzelm@384
    59
wenzelm@384
    60
fun strip ("'" :: c :: cs) = c :: strip cs
wenzelm@384
    61
  | strip ["'"] = []
wenzelm@384
    62
  | strip (c :: cs) = c :: strip cs
wenzelm@384
    63
  | strip [] = [];
wenzelm@384
    64
wenzelm@384
    65
val strip_esc = implode o strip o explode;
wenzelm@384
    66
wenzelm@384
    67
wenzelm@384
    68
fun type_name t (Infixl _) = strip_esc t
wenzelm@384
    69
  | type_name t (Infixr _) = strip_esc t
wenzelm@384
    70
  | type_name t _ = t;
wenzelm@384
    71
wenzelm@384
    72
fun infix_name c = "op " ^ strip_esc c;
wenzelm@384
    73
wenzelm@384
    74
fun const_name c (Infixl _) = infix_name c
wenzelm@384
    75
  | const_name c (Infixr _) = infix_name c
wenzelm@384
    76
  | const_name c _ = c;
wenzelm@384
    77
wenzelm@384
    78
wenzelm@384
    79
(* syn_ext_types *)
wenzelm@384
    80
clasohm@764
    81
fun syn_ext_types logtypes type_decls =
wenzelm@384
    82
  let
wenzelm@384
    83
    fun name_of (t, _, mx) = type_name t mx;
wenzelm@384
    84
wenzelm@384
    85
    fun mk_infix t p1 p2 p3 =
wenzelm@384
    86
      Mfix ("(_ " ^ t ^ "/ _)", [typeT, typeT] ---> typeT,
wenzelm@384
    87
        strip_esc t, [p1, p2], p3);
wenzelm@384
    88
wenzelm@384
    89
    fun mfix_of (_, _, NoSyn) = None
wenzelm@384
    90
      | mfix_of (t, 2, Infixl p) = Some (mk_infix t p (p + 1) p)
wenzelm@384
    91
      | mfix_of (t, 2, Infixr p) = Some (mk_infix t (p + 1) p p)
wenzelm@384
    92
      | mfix_of decl = error ("Bad mixfix declaration for type " ^
wenzelm@384
    93
          quote (name_of decl));
wenzelm@384
    94
wenzelm@384
    95
    val mfix = mapfilter mfix_of type_decls;
wenzelm@384
    96
    val xconsts = map name_of type_decls;
wenzelm@384
    97
  in
clasohm@764
    98
    syn_ext logtypes mfix xconsts ([], [], [], []) ([], [])
wenzelm@384
    99
  end;
wenzelm@384
   100
wenzelm@384
   101
wenzelm@384
   102
(* syn_ext_consts *)
wenzelm@384
   103
clasohm@764
   104
fun syn_ext_consts logtypes const_decls =
wenzelm@384
   105
  let
wenzelm@384
   106
    fun name_of (c, _, mx) = const_name c mx;
wenzelm@384
   107
wenzelm@384
   108
    fun mk_infix sy ty c p1 p2 p3 =
wenzelm@384
   109
      [Mfix ("(_ " ^ sy ^ "/ _)", ty, c, [p1, p2], p3),
wenzelm@384
   110
       Mfix ("op " ^ sy, ty, c, [], max_pri)];
wenzelm@384
   111
wenzelm@384
   112
    fun binder_typ _ (Type ("fun", [Type ("fun", [_, ty2]), ty3])) =
wenzelm@384
   113
          [Type ("idts", []), ty2] ---> ty3
wenzelm@384
   114
      | binder_typ c _ = error ("Bad type of binder " ^ quote c);
wenzelm@384
   115
wenzelm@384
   116
    fun mfix_of (_, _, NoSyn) = []
wenzelm@384
   117
      | mfix_of (c, ty, Mixfix (sy, ps, p)) = [Mfix (sy, ty, c, ps, p)]
wenzelm@384
   118
      | mfix_of (c, ty, Delimfix sy) = [Mfix (sy, ty, c, [], max_pri)]
wenzelm@384
   119
      | mfix_of (sy, ty, Infixl p) = mk_infix sy ty (infix_name sy) p (p + 1) p
wenzelm@384
   120
      | mfix_of (sy, ty, Infixr p) = mk_infix sy ty (infix_name sy) (p + 1) p p
wenzelm@887
   121
      | mfix_of (c, ty, Binder (sy, p, q)) =
wenzelm@887
   122
          [Mfix ("(3" ^ sy ^ "_./ _)", binder_typ c ty, sy, [0, p], q)];
wenzelm@384
   123
clasohm@865
   124
    fun binder (c, _, Binder (sy, _, _)) = Some (sy, c)
wenzelm@384
   125
      | binder _ = None;
wenzelm@384
   126
wenzelm@384
   127
    val mfix = flat (map mfix_of const_decls);
wenzelm@384
   128
    val xconsts = map name_of const_decls;
wenzelm@384
   129
    val binders = mapfilter binder const_decls;
wenzelm@384
   130
    val binder_trs = map mk_binder_tr binders;
wenzelm@384
   131
    val binder_trs' = map (mk_binder_tr' o swap) binders;
wenzelm@384
   132
  in
clasohm@764
   133
    syn_ext logtypes mfix xconsts ([], binder_trs, binder_trs', []) ([], [])
wenzelm@384
   134
  end;
wenzelm@384
   135
wenzelm@384
   136
wenzelm@384
   137
wenzelm@384
   138
(** Pure syntax **)
wenzelm@384
   139
wenzelm@384
   140
val pure_types =
wenzelm@384
   141
  map (fn t => (t, 0, NoSyn))
wenzelm@384
   142
    (terminals @ [logic, "type", "types", "sort", "classes", args, "idt",
wenzelm@781
   143
      "idts", "aprop", "asms", any, sprop]);
wenzelm@384
   144
wenzelm@384
   145
val pure_syntax =
wenzelm@781
   146
 [("_lambda",   "[idts, 'a] => logic",            Mixfix ("(3%_./ _)", [], 0)),
wenzelm@384
   147
  ("",          "'a => " ^ args,                  Delimfix "_"),
wenzelm@384
   148
  ("_args",     "['a, " ^ args ^ "] => " ^ args,  Delimfix "_,/ _"),
wenzelm@384
   149
  ("",          "id => idt",                      Delimfix "_"),
wenzelm@384
   150
  ("_idtyp",    "[id, type] => idt",              Mixfix ("_::_", [], 0)),
wenzelm@384
   151
  ("",          "idt => idt",                     Delimfix "'(_')"),
wenzelm@384
   152
  ("",          "idt => idts",                    Delimfix "_"),
wenzelm@384
   153
  ("_idts",     "[idt, idts] => idts",            Mixfix ("_/ _", [1, 0], 0)),
wenzelm@384
   154
  ("",          "id => aprop",                    Delimfix "_"),
wenzelm@384
   155
  ("",          "var => aprop",                   Delimfix "_"),
wenzelm@384
   156
  (applC,       "[('b => 'a), " ^ args ^ "] => aprop",
wenzelm@384
   157
    Mixfix ("(1_/(1'(_')))", [max_pri, 0], max_pri)),
wenzelm@384
   158
  ("_aprop",    "aprop => prop",                  Delimfix "PROP _"),
wenzelm@384
   159
  ("",          "prop => asms",                   Delimfix "_"),
wenzelm@384
   160
  ("_asms",     "[prop, asms] => asms",           Delimfix "_;/ _"),
wenzelm@384
   161
  ("_bigimpl",  "[asms, prop] => prop",           Mixfix ("((3[| _ |]) ==>/ _)", [0, 1], 1)),
wenzelm@842
   162
  ("_ofclass",  "[type, logic] => prop",          Delimfix "(1OFCLASS/(1'(_,/ _')))"),
wenzelm@384
   163
  ("_K",        "'a",                             NoSyn),
clasohm@624
   164
  ("",          "id => logic",                    Delimfix "_"),
clasohm@624
   165
  ("",          "var => logic",                   Delimfix "_"),
clasohm@764
   166
  ("_appl",     "[logic, args] => logic",         Mixfix ("(1_/(1'(_')))", [max_pri, 0], max_pri))]
wenzelm@384
   167
wenzelm@384
   168
end;