src/Pure/Syntax/mixfix.ML
author paulson
Thu Sep 25 12:09:41 1997 +0200 (1997-09-25)
changeset 3706 e57b5902822f
parent 3690 3f7053bf4237
child 3815 7e8847f8f3a4
permissions -rw-r--r--
Generalized and exported biresolution_from_nets_tac to allow the declaration
of Clarify_tac
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@2256
    31
  val pure_sym_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
wenzelm@2696
   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@2381
   147
    val binder_trs' = map (apsnd fix_tr' o mk_binder_tr' o swap) binders;
wenzelm@384
   148
  in
wenzelm@2696
   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,
wenzelm@3690
   159
      "pttrn", "pttrns", "idt", "idts", "aprop", "asms", any, sprop, "dummy"]);
wenzelm@384
   160
wenzelm@384
   161
val pure_syntax =
wenzelm@3690
   162
 [("_lambda",      "[pttrns, 'a] => logic",         Mixfix ("(3%_./ _)", [0, 3], 3)),
wenzelm@2696
   163
  ("_abs",         "'a",                            NoSyn),
wenzelm@2696
   164
  ("",             "'a => " ^ args,                 Delimfix "_"),
wenzelm@2696
   165
  ("_args",        "['a, " ^ args ^ "] => " ^ args, Delimfix "_,/ _"),
wenzelm@2696
   166
  ("",             "id => idt",                     Delimfix "_"),
wenzelm@2696
   167
  ("_idtyp",       "[id, type] => idt",             Mixfix ("_::_", [], 0)),
wenzelm@2696
   168
  ("",             "idt => idt",                    Delimfix "'(_')"),
wenzelm@3690
   169
  ("",             "idt => idts",                   Delimfix "_"),
wenzelm@3690
   170
  ("_idts",        "[idt, idts] => idts",           Mixfix ("_/ _", [1, 0], 0)),
wenzelm@2696
   171
  ("",             "idt => pttrn",                  Delimfix "_"),
wenzelm@3690
   172
  ("",             "pttrn => pttrns",               Delimfix "_"),
wenzelm@3690
   173
  ("_pttrns",      "[pttrn, pttrns] => pttrns",     Mixfix ("_/ _", [1, 0], 0)),
wenzelm@2696
   174
  ("",             "id => aprop",                   Delimfix "_"),
wenzelm@2696
   175
  ("",             "var => aprop",                  Delimfix "_"),
wenzelm@2696
   176
  ("_aprop",       "aprop => prop",                 Delimfix "PROP _"),
wenzelm@2696
   177
  ("",             "prop => asms",                  Delimfix "_"),
wenzelm@2696
   178
  ("_asms",        "[prop, asms] => asms",          Delimfix "_;/ _"),
wenzelm@2696
   179
  ("_bigimpl",     "[asms, prop] => prop",          Mixfix ("((3[| _ |])/ ==> _)", [0, 1], 1)),
wenzelm@2696
   180
  ("_ofclass",     "[type, logic] => prop",         Delimfix "(1OFCLASS/(1'(_,/ _')))"),
wenzelm@2696
   181
  ("_mk_ofclass",  "_",                             NoSyn),
wenzelm@2696
   182
  ("_mk_ofclassS", "_",                             NoSyn),
wenzelm@2696
   183
  ("_K",           "_",                             NoSyn),
wenzelm@2696
   184
  ("",             "id => logic",                   Delimfix "_"),
wenzelm@2696
   185
  ("",             "var => logic",                  Delimfix "_")];
wenzelm@384
   186
clasohm@1181
   187
val pure_appl_syntax =
wenzelm@2696
   188
 [("_appl", "[('b => 'a), args] => logic", Mixfix ("(1_/(1'(_')))", [max_pri, 0], max_pri)),
wenzelm@2696
   189
  ("_appl", "[('b => 'a), args] => aprop", Mixfix ("(1_/(1'(_')))", [max_pri, 0], max_pri))];
clasohm@1181
   190
clasohm@1181
   191
val pure_applC_syntax =
wenzelm@2696
   192
 [("",       "'a => cargs",                  Delimfix "_"),
wenzelm@2696
   193
  ("_cargs", "['a, cargs] => cargs",         Mixfix ("_/ _", [max_pri, max_pri], max_pri)),
wenzelm@2696
   194
  ("_applC", "[('b => 'a), cargs] => logic", Mixfix ("(1_/ _)", [max_pri, max_pri], max_pri - 1)),
wenzelm@2696
   195
  ("_applC", "[('b => 'a), cargs] => aprop", Mixfix ("(1_/ _)", [max_pri, max_pri], max_pri - 1))];
wenzelm@2199
   196
wenzelm@2256
   197
val pure_sym_syntax =
wenzelm@2696
   198
 [("fun",      "[type, type] => type",  Mixfix ("(_/ \\<Rightarrow> _)", [1, 0], 0)),
wenzelm@2696
   199
  ("_bracket", "[types, type] => type", Mixfix ("([_]/ \\<Rightarrow> _)", [0, 0], 0)),
wenzelm@2767
   200
  ("_ofsort",  "[tid, sort] => type",   Mixfix ("_\\<Colon>_", [max_pri, 0], max_pri)),
wenzelm@2767
   201
  ("_constrain", "['a, type] => 'a",    Mixfix ("_\\<Colon>_", [4, 0], 3)),
wenzelm@2767
   202
  ("_idtyp",   "[id, type] => idt",     Mixfix ("_\\<Colon>_", [], 0)),
wenzelm@3690
   203
  ("_lambda",  "[pttrns, 'a] => logic", Mixfix ("(3\\<lambda>_./ _)", [0, 3], 3)),
wenzelm@2696
   204
  ("==>",      "[prop, prop] => prop",  InfixrName ("\\<Midarrow>\\<Rightarrow>", 1)),
wenzelm@2696
   205
  ("_bigimpl", "[asms, prop] => prop",  Mixfix ("((3\\<lbrakk>_\\<rbrakk>)/ \\<Midarrow>\\<Rightarrow> _)", [0, 1], 1)),
wenzelm@2696
   206
  ("==",       "['a::{}, 'a] => prop",  InfixrName ("\\<equiv>", 2)),
wenzelm@2696
   207
  ("!!",       "[idts, prop] => prop",  Mixfix ("(3\\<And>_./ _)", [0, 0], 0))];
wenzelm@2199
   208
wenzelm@384
   209
end;