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