src/Pure/Syntax/mixfix.ML
author skalberg
Sun Feb 13 17:15:14 2005 +0100 (2005-02-13)
changeset 15531 08c8dad8e399
parent 15421 fcf747c0b6b8
child 15570 8d8c70b41bab
permissions -rw-r--r--
Deleted Library.option type.
     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 
    49 structure Mixfix : MIXFIX =
    50 struct
    51 
    52 
    53 (** mixfix declarations **)
    54 
    55 datatype mixfix =
    56   NoSyn |
    57   Mixfix of string * int list * int |
    58   Delimfix of string |
    59   InfixName of string * int |
    60   InfixlName of string * int |
    61   InfixrName of string * int |
    62   Infix of int |
    63   Infixl of int |
    64   Infixr of int |
    65   Binder of string * int * int;
    66 
    67 val literal = Delimfix o SynExt.escape_mfix;
    68 
    69 fun no_syn (x, y) = (x, y, NoSyn);
    70 
    71 
    72 (* string_of_mixfix *)
    73 
    74 val parens = enclose "(" ")";
    75 val brackets = enclose "[" "]";
    76 val spaces = space_implode " ";
    77 
    78 fun string_of_mixfix NoSyn = ""
    79   | string_of_mixfix (Mixfix (sy, ps, p)) =
    80       parens (spaces [quote sy, brackets (commas (map string_of_int ps)), string_of_int p])
    81   | string_of_mixfix (Delimfix sy) = parens (quote sy)
    82   | string_of_mixfix (InfixName (sy, p)) = parens (spaces ["infix", quote sy, string_of_int p])
    83   | string_of_mixfix (InfixlName (sy, p)) = parens (spaces ["infixl", quote sy, string_of_int p])
    84   | string_of_mixfix (InfixrName (sy, p)) = parens (spaces ["infixl", quote sy, string_of_int p])
    85   | string_of_mixfix (Infix p) = parens (spaces ["infix", string_of_int p])
    86   | string_of_mixfix (Infixl p) = parens (spaces ["infixl", string_of_int p])
    87   | string_of_mixfix (Infixr p) = parens (spaces ["infixr", string_of_int p])
    88   | string_of_mixfix (Binder (sy, p1, p2)) =
    89       parens (spaces ["binder", brackets (string_of_int p1), string_of_int p2]);
    90 
    91 
    92 (* syntax specifications *)
    93 
    94 fun strip ("'" :: c :: cs) = c :: strip cs
    95   | strip ["'"] = []
    96   | strip (c :: cs) = c :: strip cs
    97   | strip [] = [];
    98 
    99 val strip_esc = implode o strip o Symbol.explode;
   100 
   101 fun type_name t (InfixName _) = t
   102   | type_name t (InfixlName _) = t
   103   | type_name t (InfixrName _) = t
   104   | type_name t (Infix _) = strip_esc t
   105   | type_name t (Infixl _) = strip_esc t
   106   | type_name t (Infixr _) = strip_esc t
   107   | type_name t _ = t;
   108 
   109 fun const_name c (InfixName _) = c
   110   | const_name c (InfixlName _) = c
   111   | const_name c (InfixrName _) = c
   112   | const_name c (Infix _) = "op " ^ strip_esc c
   113   | const_name c (Infixl _) = "op " ^ strip_esc c
   114   | const_name c (Infixr _) = "op " ^ strip_esc c
   115   | const_name c _ = c;
   116 
   117 fun fix_mixfix c (Infix p) = (InfixName (c, p))
   118   | fix_mixfix c (Infixl p) = (InfixlName (c, p))
   119   | fix_mixfix c (Infixr p) = (InfixrName (c, p))
   120   | fix_mixfix _ mx = mx;
   121 
   122 fun mixfix_args NoSyn = 0
   123   | mixfix_args (Mixfix (sy, _, _)) = SynExt.mfix_args sy
   124   | mixfix_args (Delimfix sy) = SynExt.mfix_args sy
   125   | mixfix_args (InfixName (sy, _)) = 2 + SynExt.mfix_args sy
   126   | mixfix_args (InfixlName (sy, _)) = 2 + SynExt.mfix_args sy
   127   | mixfix_args (InfixrName (sy, _)) = 2 + SynExt.mfix_args sy
   128   | mixfix_args (*old infix or binder*) _ = 2;
   129 
   130 
   131 (* syn_ext_types *)
   132 
   133 fun syn_ext_types type_decls =
   134   let
   135     fun name_of (t, _, mx) = type_name t mx;
   136 
   137     fun mk_infix sy t p1 p2 p3 =
   138       SynExt.Mfix ("(_ " ^ sy ^ "/ _)",
   139         [SynExt.typeT, SynExt.typeT] ---> SynExt.typeT, t, [p1, p2], p3);
   140 
   141     fun mfix_of decl =
   142       let val t = name_of decl in
   143         (case decl of
   144           (_, _, NoSyn) => NONE
   145         | (_, 2, InfixName (sy, p)) => SOME (mk_infix sy t (p + 1) (p + 1) p)
   146         | (_, 2, InfixlName (sy, p)) => SOME (mk_infix sy t p (p + 1) p)
   147         | (_, 2, InfixrName (sy, p)) => SOME (mk_infix sy t (p + 1) p p)
   148         | (sy, 2, Infix p) => SOME (mk_infix sy t (p + 1) (p + 1) p)
   149         | (sy, 2, Infixl p) => SOME (mk_infix sy t p (p + 1) p)
   150         | (sy, 2, Infixr p) => SOME (mk_infix sy t (p + 1) p p)
   151         | _ => error ("Bad mixfix declaration for type: " ^ quote t))
   152       end;
   153 
   154     val mfix = mapfilter mfix_of type_decls;
   155     val xconsts = map name_of type_decls;
   156   in SynExt.syn_ext mfix xconsts ([], [], [], []) [] ([], []) end;
   157 
   158 
   159 (* syn_ext_consts *)
   160 
   161 fun syn_ext_consts is_logtype const_decls =
   162   let
   163     fun name_of (c, _, mx) = const_name c mx;
   164 
   165     fun mk_infix sy ty c p1 p2 p3 =
   166       [SynExt.Mfix ("op " ^ sy, ty, c, [], SynExt.max_pri),
   167        SynExt.Mfix ("(_ " ^ sy ^ "/ _)", ty, c, [p1, p2], p3)];
   168 
   169     fun binder_typ _ (Type ("fun", [Type ("fun", [_, ty2]), ty3])) =
   170           [Type ("idts", []), ty2] ---> ty3
   171       | binder_typ c _ = error ("Bad type of binder: " ^ quote c);
   172 
   173     fun mfix_of decl =
   174       let val c = name_of decl in
   175         (case decl of
   176           (_, _, NoSyn) => []
   177         | (_, ty, Mixfix (sy, ps, p)) => [SynExt.Mfix (sy, ty, c, ps, p)]
   178         | (_, ty, Delimfix sy) => [SynExt.Mfix (sy, ty, c, [], SynExt.max_pri)]
   179         | (_, ty, InfixName (sy, p)) => mk_infix sy ty c (p + 1) (p + 1) p
   180         | (_, ty, InfixlName (sy, p)) => mk_infix sy ty c p (p + 1) p
   181         | (_, ty, InfixrName (sy, p)) => mk_infix sy ty c (p + 1) p p
   182         | (sy, ty, Infix p) => mk_infix sy ty c (p + 1) (p + 1) p
   183         | (sy, ty, Infixl p) => mk_infix sy ty c p (p + 1) p
   184         | (sy, ty, Infixr p) => mk_infix sy ty c (p + 1) p p
   185         | (_, ty, Binder (sy, p, q)) =>
   186             [SynExt.Mfix ("(3" ^ sy ^ "_./ _)", binder_typ c ty, sy, [0, p], q)])
   187     end;
   188 
   189     fun binder (c, _, Binder (sy, _, _)) = SOME (sy, c)
   190       | binder _ = NONE;
   191 
   192     val mfix = flat (map mfix_of const_decls);
   193     val xconsts = map name_of const_decls;
   194     val binders = mapfilter binder const_decls;
   195     val binder_trs = map SynTrans.mk_binder_tr binders;
   196     val binder_trs' =
   197       map (apsnd SynTrans.non_typed_tr' o SynTrans.mk_binder_tr' o swap) binders;
   198   in
   199     SynExt.syn_ext' true is_logtype
   200       mfix xconsts ([], binder_trs, binder_trs', []) [] ([], [])
   201   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   ("_asm",        "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;