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