src/Pure/Syntax/mixfix.ML
author wenzelm
Tue Jun 01 12:35:46 2004 +0200 (2004-06-01)
changeset 14855 a58abaad4f45
parent 14846 b1fcade3880b
child 14903 d264b8ad3eec
permissions -rw-r--r--
removed obsolete sort 'logic' and '=?=' syntax;
     1 (*  Title:      Pure/Syntax/mixfix.ML
     2     ID:         $Id$
     3     Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 
     6 Mixfix declarations, infixes, binders. Also parts of the Pure syntax.
     7 *)
     8 
     9 signature MIXFIX0 =
    10 sig
    11   datatype mixfix =
    12     NoSyn |
    13     Mixfix of string * int list * int |
    14     Delimfix of string |
    15     InfixName of string * int |
    16     InfixlName of string * int |
    17     InfixrName of string * int |
    18     Infix of int |
    19     Infixl of int |
    20     Infixr of int |
    21     Binder of string * int * int
    22 end;
    23 
    24 signature MIXFIX1 =
    25 sig
    26   include MIXFIX0
    27   val literal: string -> mixfix
    28   val no_syn: 'a * 'b -> 'a * 'b * mixfix
    29   val string_of_mixfix: mixfix -> string
    30   val type_name: string -> mixfix -> string
    31   val const_name: string -> mixfix -> string
    32   val fix_mixfix: string -> mixfix -> mixfix
    33   val mixfix_args: mixfix -> int
    34   val pure_nonterms: string list
    35   val pure_syntax: (string * string * mixfix) list
    36   val pure_syntax_output: (string * string * mixfix) list
    37   val pure_appl_syntax: (string * string * mixfix) list
    38   val pure_applC_syntax: (string * string * mixfix) list
    39   val pure_xsym_syntax: (string * string * mixfix) list
    40 end;
    41 
    42 signature MIXFIX =
    43 sig
    44   include MIXFIX1
    45   val syn_ext_types: string list -> (string * int * mixfix) list
    46     -> SynExt.syn_ext
    47   val syn_ext_consts: string list -> (string * typ * mixfix) list
    48     -> SynExt.syn_ext
    49 end;
    50 
    51 
    52 structure Mixfix : MIXFIX =
    53 struct
    54 
    55 
    56 (** mixfix declarations **)
    57 
    58 datatype mixfix =
    59   NoSyn |
    60   Mixfix of string * int list * int |
    61   Delimfix of string |
    62   InfixName of string * int |
    63   InfixlName of string * int |
    64   InfixrName of string * int |
    65   Infix of int |
    66   Infixl of int |
    67   Infixr of int |
    68   Binder of string * int * int;
    69 
    70 val literal = Delimfix o SynExt.escape_mfix;
    71 
    72 fun no_syn (x, y) = (x, y, NoSyn);
    73 
    74 
    75 (* string_of_mixfix *)
    76 
    77 val parens = enclose "(" ")";
    78 val brackets = enclose "[" "]";
    79 val spaces = space_implode " ";
    80 
    81 fun string_of_mixfix NoSyn = ""
    82   | string_of_mixfix (Mixfix (sy, ps, p)) =
    83       parens (spaces [quote sy, brackets (commas (map string_of_int ps)), string_of_int p])
    84   | string_of_mixfix (Delimfix sy) = parens (quote sy)
    85   | string_of_mixfix (InfixName (sy, p)) = parens (spaces ["infix", quote sy, string_of_int p])
    86   | string_of_mixfix (InfixlName (sy, p)) = parens (spaces ["infixl", quote sy, string_of_int p])
    87   | string_of_mixfix (InfixrName (sy, p)) = parens (spaces ["infixl", quote sy, string_of_int p])
    88   | string_of_mixfix (Infix p) = parens (spaces ["infix", string_of_int p])
    89   | string_of_mixfix (Infixl p) = parens (spaces ["infixl", string_of_int p])
    90   | string_of_mixfix (Infixr p) = parens (spaces ["infixr", string_of_int p])
    91   | string_of_mixfix (Binder (sy, p1, p2)) =
    92       parens (spaces ["binder", brackets (string_of_int p1), string_of_int p2]);
    93 
    94 
    95 (* syntax specifications *)
    96 
    97 fun strip ("'" :: c :: cs) = c :: strip cs
    98   | strip ["'"] = []
    99   | strip (c :: cs) = c :: strip cs
   100   | strip [] = [];
   101 
   102 val strip_esc = implode o strip o Symbol.explode;
   103 
   104 fun type_name t (InfixName _) = t
   105   | type_name t (InfixlName _) = t
   106   | type_name t (InfixrName _) = t
   107   | type_name t (Infix _) = strip_esc t
   108   | type_name t (Infixl _) = strip_esc t
   109   | type_name t (Infixr _) = strip_esc t
   110   | type_name t _ = t;
   111 
   112 fun const_name c (InfixName _) = c
   113   | const_name c (InfixlName _) = c
   114   | const_name c (InfixrName _) = c
   115   | const_name c (Infix _) = "op " ^ strip_esc c
   116   | const_name c (Infixl _) = "op " ^ strip_esc c
   117   | const_name c (Infixr _) = "op " ^ strip_esc c
   118   | const_name c _ = c;
   119 
   120 fun fix_mixfix c (Infix p) = (InfixName (c, p))
   121   | fix_mixfix c (Infixl p) = (InfixlName (c, p))
   122   | fix_mixfix c (Infixr p) = (InfixrName (c, p))
   123   | fix_mixfix _ mx = mx;
   124 
   125 fun mixfix_args NoSyn = 0
   126   | mixfix_args (Mixfix (sy, _, _)) = SynExt.mfix_args sy
   127   | mixfix_args (Delimfix sy) = SynExt.mfix_args sy
   128   | mixfix_args (InfixName (sy, _)) = 2 + SynExt.mfix_args sy
   129   | mixfix_args (InfixlName (sy, _)) = 2 + SynExt.mfix_args sy
   130   | mixfix_args (InfixrName (sy, _)) = 2 + SynExt.mfix_args sy
   131   | mixfix_args (*old infix or binder*) _ = 2;
   132 
   133 
   134 (* syn_ext_types *)
   135 
   136 fun syn_ext_types logtypes type_decls =
   137   let
   138     fun name_of (t, _, mx) = type_name t mx;
   139 
   140     fun mk_infix sy t p1 p2 p3 =
   141       SynExt.Mfix ("(_ " ^ sy ^ "/ _)",
   142         [SynExt.typeT, SynExt.typeT] ---> SynExt.typeT, t, [p1, p2], p3);
   143 
   144     fun mfix_of decl =
   145       let val t = name_of decl in
   146         (case decl of
   147           (_, _, NoSyn) => None
   148         | (_, 2, InfixName (sy, p)) => Some (mk_infix sy t (p + 1) (p + 1) p)
   149         | (_, 2, InfixlName (sy, p)) => Some (mk_infix sy t p (p + 1) p)
   150         | (_, 2, InfixrName (sy, p)) => Some (mk_infix sy t (p + 1) p p)
   151         | (sy, 2, Infix p) => Some (mk_infix sy t (p + 1) (p + 1) p)
   152         | (sy, 2, Infixl p) => Some (mk_infix sy t p (p + 1) p)
   153         | (sy, 2, Infixr p) => Some (mk_infix sy t (p + 1) p p)
   154         | _ => error ("Bad mixfix declaration for type " ^ quote t))
   155       end;
   156 
   157     val mfix = mapfilter mfix_of type_decls;
   158     val xconsts = map name_of type_decls;
   159   in SynExt.syn_ext logtypes mfix xconsts ([], [], [], []) [] ([], []) end;
   160 
   161 
   162 (* syn_ext_consts *)
   163 
   164 fun syn_ext_consts logtypes const_decls =
   165   let
   166     fun name_of (c, _, mx) = const_name c mx;
   167 
   168     fun mk_infix sy ty c p1 p2 p3 =
   169       [SynExt.Mfix ("op " ^ sy, ty, c, [], SynExt.max_pri),
   170        SynExt.Mfix ("(_ " ^ sy ^ "/ _)", ty, c, [p1, p2], p3)];
   171 
   172     fun binder_typ _ (Type ("fun", [Type ("fun", [_, ty2]), ty3])) =
   173           [Type ("idts", []), ty2] ---> ty3
   174       | binder_typ c _ = error ("Bad type of binder " ^ quote c);
   175 
   176     fun mfix_of decl =
   177       let val c = name_of decl in
   178         (case decl of
   179           (_, _, NoSyn) => []
   180         | (_, ty, Mixfix (sy, ps, p)) => [SynExt.Mfix (sy, ty, c, ps, p)]
   181         | (_, ty, Delimfix sy) => [SynExt.Mfix (sy, ty, c, [], SynExt.max_pri)]
   182         | (_, ty, InfixName (sy, p)) => mk_infix sy ty c (p + 1) (p + 1) p
   183         | (_, ty, InfixlName (sy, p)) => mk_infix sy ty c p (p + 1) p
   184         | (_, ty, InfixrName (sy, p)) => mk_infix sy ty c (p + 1) p p
   185         | (sy, ty, Infix p) => mk_infix sy ty c (p + 1) (p + 1) p
   186         | (sy, ty, Infixl p) => mk_infix sy ty c p (p + 1) p
   187         | (sy, ty, Infixr p) => mk_infix sy ty c (p + 1) p p
   188         | (_, ty, Binder (sy, p, q)) =>
   189             [SynExt.Mfix ("(3" ^ sy ^ "_./ _)", binder_typ c ty, sy, [0, p], q)])
   190     end;
   191 
   192     fun binder (c, _, Binder (sy, _, _)) = Some (sy, c)
   193       | binder _ = None;
   194 
   195     val mfix = flat (map mfix_of const_decls);
   196     val xconsts = map name_of const_decls;
   197     val binders = mapfilter binder const_decls;
   198     val binder_trs = map SynTrans.mk_binder_tr binders;
   199     val binder_trs' =
   200       map (apsnd SynTrans.non_typed_tr' o SynTrans.mk_binder_tr' o swap) binders;
   201   in SynExt.syn_ext logtypes mfix xconsts ([], binder_trs, binder_trs', []) [] ([], []) end;
   202 
   203 
   204 
   205 (** Pure syntax **)
   206 
   207 val pure_nonterms =
   208   (Lexicon.terminals @ [SynExt.logic, "type", "types", "sort", "classes",
   209     SynExt.args, SynExt.cargs, "pttrn", "pttrns", "idt", "idts", "aprop",
   210     "asms", SynExt.any, SynExt.sprop, "num_const", "index", "struct"]);
   211 
   212 val pure_syntax =
   213  [("_lambda",     "[pttrns, 'a] => logic",     Mixfix ("(3%_./ _)", [0, 3], 3)),
   214   ("_abs",        "'a",                        NoSyn),
   215   ("",            "'a => " ^ SynExt.args,      Delimfix "_"),
   216   ("_args",       "['a, " ^ SynExt.args ^ "] => " ^ SynExt.args, Delimfix "_,/ _"),
   217   ("",            "id => idt",                 Delimfix "_"),
   218   ("_idtyp",      "[id, type] => idt",         Mixfix ("_::_", [], 0)),
   219   ("",            "idt => idt",                Delimfix "'(_')"),
   220   ("",            "idt => idts",               Delimfix "_"),
   221   ("_idts",       "[idt, idts] => idts",       Mixfix ("_/ _", [1, 0], 0)),
   222   ("",            "idt => pttrn",              Delimfix "_"),
   223   ("",            "pttrn => pttrns",           Delimfix "_"),
   224   ("_pttrns",     "[pttrn, pttrns] => pttrns", Mixfix ("_/ _", [1, 0], 0)),
   225   ("",            "id => aprop",               Delimfix "_"),
   226   ("",            "longid => aprop",           Delimfix "_"),
   227   ("",            "var => aprop",              Delimfix "_"),
   228   ("_DDDOT",      "aprop",                     Delimfix "..."),
   229   ("_aprop",      "aprop => prop",             Delimfix "PROP _"),
   230   ("",            "prop => asms",              Delimfix "_"),
   231   ("_asms",       "[prop, asms] => asms",      Delimfix "_;/ _"),
   232   ("_bigimpl",    "[asms, prop] => prop",      Mixfix ("((3[| _ |])/ ==> _)", [0, 1], 1)),
   233   ("_ofclass",    "[type, logic] => prop",     Delimfix "(1OFCLASS/(1'(_,/ _')))"),
   234   ("_mk_ofclass", "_",                         NoSyn),
   235   ("_TYPE",       "type => logic",             Delimfix "(1TYPE/(1'(_')))"),
   236   ("_K",          "_",                         NoSyn),
   237   ("",            "id => logic",               Delimfix "_"),
   238   ("",            "longid => logic",           Delimfix "_"),
   239   ("",            "var => logic",              Delimfix "_"),
   240   ("_DDDOT",      "logic",                     Delimfix "..."),
   241   ("_constify",   "num => num_const",          Delimfix "_"),
   242   ("_indexnum",   "num_const => index",        Delimfix "\\<^sub>_"),
   243   ("_index",      "logic => index",            Delimfix "(00\\<^bsub>_\\<^esub>)"),
   244   ("_indexdefault", "index",                   Delimfix ""),
   245   ("_indexvar",   "index",                     Delimfix "'\\<index>"),
   246   ("_struct",     "index => logic",            Mixfix ("\\<struct>_", [1000], 1000))];
   247 
   248 val pure_syntax_output =
   249  [("Goal", "prop => prop", Mixfix ("_", [0], 0)),
   250   ("_meta_conjunction", "prop => prop => prop", InfixrName ("&&", 2))];
   251 
   252 val pure_appl_syntax =
   253  [("_appl", "[('b => 'a), args] => logic", Mixfix ("(1_/(1'(_')))", [SynExt.max_pri, 0], SynExt.max_pri)),
   254   ("_appl", "[('b => 'a), args] => aprop", Mixfix ("(1_/(1'(_')))", [SynExt.max_pri, 0], SynExt.max_pri))];
   255 
   256 val pure_applC_syntax =
   257  [("",       "'a => cargs",                  Delimfix "_"),
   258   ("_cargs", "['a, cargs] => cargs",         Mixfix ("_/ _", [SynExt.max_pri, SynExt.max_pri], SynExt.max_pri)),
   259   ("_applC", "[('b => 'a), cargs] => logic", Mixfix ("(1_/ _)", [SynExt.max_pri, SynExt.max_pri], SynExt.max_pri - 1)),
   260   ("_applC", "[('b => 'a), cargs] => aprop", Mixfix ("(1_/ _)", [SynExt.max_pri, SynExt.max_pri], SynExt.max_pri - 1))];
   261 
   262 val pure_xsym_syntax =
   263  [("fun",      "[type, type] => type",  Mixfix ("(_/ \\<Rightarrow> _)", [1, 0], 0)),
   264   ("_bracket", "[types, type] => type", Mixfix ("([_]/ \\<Rightarrow> _)", [0, 0], 0)),
   265   ("_ofsort",  "[tid, sort] => type",   Mixfix ("_\\<Colon>_", [SynExt.max_pri, 0], SynExt.max_pri)),
   266   ("_constrain", "['a, type] => 'a",    Mixfix ("_\\<Colon>_", [4, 0], 3)),
   267   ("_idtyp",   "[id, type] => idt",     Mixfix ("_\\<Colon>_", [], 0)),
   268   ("_lambda",  "[pttrns, 'a] => logic", Mixfix ("(3\\<lambda>_./ _)", [0, 3], 3)),
   269   ("==",       "['a, 'a] => prop",      InfixrName ("\\<equiv>", 2)),
   270   ("!!",       "[idts, prop] => prop",  Mixfix ("(3\\<And>_./ _)", [0, 0], 0)),
   271   ("==>",      "[prop, prop] => prop",  InfixrName ("\\<Longrightarrow>", 1)),
   272   ("_DDDOT",   "aprop",                 Delimfix "\\<dots>"),
   273   ("_bigimpl", "[asms, prop] => prop",  Mixfix ("((1\\<lbrakk>_\\<rbrakk>)/ \\<Longrightarrow> _)", [0, 1], 1)),
   274   ("_DDDOT",   "logic",                 Delimfix "\\<dots>")];
   275 
   276 end;