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