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