src/Pure/Syntax/mixfix.ML
author wenzelm
Wed Oct 31 21:59:07 2001 +0100 (2001-10-31)
changeset 12004 1703de633aaf
parent 11978 a394d3e9c8bb
child 12095 935e29914f93
permissions -rw-r--r--
IsarThy.theorem_i: no locale;
     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 no_syn: 'a * 'b -> 'a * 'b * mixfix
    27   val string_of_mixfix: mixfix -> string
    28   val type_name: string -> mixfix -> string
    29   val const_name: string -> mixfix -> string
    30   val fix_mixfix: string -> mixfix -> mixfix
    31   val mixfix_args: mixfix -> int
    32   val pure_nonterms: string list
    33   val pure_syntax: (string * string * mixfix) list
    34   val pure_appl_syntax: (string * string * mixfix) list
    35   val pure_applC_syntax: (string * string * mixfix) list
    36   val pure_sym_syntax:  (string * string * mixfix) list
    37   val pure_xsym_syntax: (string * string * mixfix) list
    38 end;
    39 
    40 signature MIXFIX =
    41 sig
    42   include MIXFIX1
    43   val syn_ext_types: string list -> (string * int * mixfix) list
    44     -> SynExt.syn_ext
    45   val syn_ext_consts: string list -> (string * typ * mixfix) list
    46     -> 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 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", brackets (string_of_int p1), string_of_int p2]);
    89 
    90 
    91 (* type / const names *)
    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 type_name t (InfixName _) = t
   101   | type_name t (InfixlName _) = t
   102   | type_name t (InfixrName _) = t
   103   | type_name t (Infix _) = strip_esc t
   104   | type_name t (Infixl _) = strip_esc t
   105   | type_name t (Infixr _) = strip_esc t
   106   | type_name t _ = t;
   107 
   108 fun const_name c (InfixName _) = c
   109   | const_name c (InfixlName _) = c
   110   | const_name c (InfixrName _) = c
   111   | const_name c (Infix _) = "op " ^ strip_esc c
   112   | const_name c (Infixl _) = "op " ^ strip_esc c
   113   | const_name c (Infixr _) = "op " ^ strip_esc c
   114   | const_name c _ = c;
   115 
   116 fun fix_mixfix c (Infix p) = (InfixName (c, p))
   117   | fix_mixfix c (Infixl p) = (InfixlName (c, p))
   118   | fix_mixfix c (Infixr p) = (InfixrName (c, p))
   119   | fix_mixfix _ mx = mx;
   120 
   121 
   122 (* mixfix_args *)
   123 
   124 fun mixfix_args NoSyn = 0
   125   | mixfix_args (Mixfix (sy, _, _)) = SynExt.mfix_args sy
   126   | mixfix_args (Delimfix sy) = SynExt.mfix_args sy
   127   | mixfix_args (*infix or binder*) _ = 2;
   128 
   129 
   130 (* syn_ext_types *)
   131 
   132 fun syn_ext_types logtypes type_decls =
   133   let
   134     fun name_of (t, _, mx) = type_name t mx;
   135 
   136     fun mk_infix sy t p1 p2 p3 =
   137       SynExt.Mfix ("(_ " ^ sy ^ "/ _)",
   138         [SynExt.typeT, SynExt.typeT] ---> SynExt.typeT, t, [p1, p2], p3);
   139 
   140     fun mfix_of decl =
   141       let val t = name_of decl in
   142         (case decl of
   143           (_, _, NoSyn) => None
   144         | (_, 2, InfixName (sy, p)) => Some (mk_infix sy t (p + 1) (p + 1) p)
   145         | (_, 2, InfixlName (sy, p)) => Some (mk_infix sy t p (p + 1) p)
   146         | (_, 2, InfixrName (sy, p)) => Some (mk_infix sy t (p + 1) p p)
   147         | (sy, 2, Infix p) => Some (mk_infix sy t (p + 1) (p + 1) p)
   148         | (sy, 2, Infixl p) => Some (mk_infix sy t p (p + 1) p)
   149         | (sy, 2, Infixr p) => Some (mk_infix sy t (p + 1) p p)
   150         | _ => error ("Bad mixfix declaration for type " ^ quote t))
   151       end;
   152 
   153     val mfix = mapfilter mfix_of type_decls;
   154     val xconsts = map name_of type_decls;
   155   in SynExt.syn_ext logtypes mfix xconsts ([], [], [], []) [] ([], []) end;
   156 
   157 
   158 (* syn_ext_consts *)
   159 
   160 fun syn_ext_consts logtypes const_decls =
   161   let
   162     fun name_of (c, _, mx) = const_name c mx;
   163 
   164     fun mk_infix sy ty c p1 p2 p3 =
   165       [SynExt.Mfix ("op " ^ sy, ty, c, [], SynExt.max_pri),
   166        SynExt.Mfix ("(_ " ^ sy ^ "/ _)", ty, c, [p1, p2], p3)];
   167 
   168     fun binder_typ _ (Type ("fun", [Type ("fun", [_, ty2]), ty3])) =
   169           [Type ("idts", []), ty2] ---> ty3
   170       | binder_typ c _ = error ("Bad type of binder " ^ quote c);
   171 
   172     fun mfix_of decl =
   173       let val c = name_of decl in
   174         (case decl of
   175           (_, _, NoSyn) => []
   176         | (_, ty, Mixfix (sy, ps, p)) => [SynExt.Mfix (sy, ty, c, ps, p)]
   177         | (_, ty, Delimfix sy) => [SynExt.Mfix (sy, ty, c, [], SynExt.max_pri)]
   178         | (_, ty, InfixName (sy, p)) => mk_infix sy ty c (p + 1) (p + 1) p
   179         | (_, ty, InfixlName (sy, p)) => mk_infix sy ty c p (p + 1) p
   180         | (_, ty, InfixrName (sy, p)) => mk_infix sy ty c (p + 1) p p
   181         | (sy, ty, Infix p) => mk_infix sy ty c (p + 1) (p + 1) p
   182         | (sy, ty, Infixl p) => mk_infix sy ty c p (p + 1) p
   183         | (sy, ty, Infixr p) => mk_infix sy ty c (p + 1) p p
   184         | (_, ty, Binder (sy, p, q)) =>
   185             [SynExt.Mfix ("(3" ^ sy ^ "_./ _)", binder_typ c ty, sy, [0, p], q)])
   186     end;
   187 
   188     fun binder (c, _, Binder (sy, _, _)) = Some (sy, c)
   189       | binder _ = None;
   190 
   191     val mfix = flat (map mfix_of const_decls);
   192     val xconsts = map name_of const_decls;
   193     val binders = mapfilter binder const_decls;
   194     val binder_trs = map SynTrans.mk_binder_tr binders;
   195     val binder_trs' = map (apsnd SynExt.fix_tr' o SynTrans.mk_binder_tr' o swap) binders;
   196   in SynExt.syn_ext logtypes mfix xconsts ([], binder_trs, binder_trs', []) [] ([], []) end;
   197 
   198 
   199 
   200 (** Pure syntax **)
   201 
   202 val pure_nonterms =
   203   (Lexicon.terminals @ [SynExt.logic, "type", "types", "sort", "classes",
   204     SynExt.args, SynExt.cargs, "pttrn", "pttrns", "idt", "idts", "aprop",
   205     "asms", SynExt.any, SynExt.sprop]);
   206 
   207 val pure_syntax =
   208  [("_lambda",      "[pttrns, 'a] => logic",         Mixfix ("(3%_./ _)", [0, 3], 3)),
   209   ("_abs",         "'a",                            NoSyn),
   210   ("",             "'a => " ^ SynExt.args,          Delimfix "_"),
   211   ("_args",        "['a, " ^ SynExt.args ^ "] => " ^ SynExt.args, Delimfix "_,/ _"),
   212   ("",             "id => idt",                     Delimfix "_"),
   213   ("_idtyp",       "[id, type] => idt",             Mixfix ("_::_", [], 0)),
   214   ("",             "idt => idt",                    Delimfix "'(_')"),
   215   ("",             "idt => idts",                   Delimfix "_"),
   216   ("_idts",        "[idt, idts] => idts",           Mixfix ("_/ _", [1, 0], 0)),
   217   ("",             "idt => pttrn",                  Delimfix "_"),
   218   ("",             "pttrn => pttrns",               Delimfix "_"),
   219   ("_pttrns",      "[pttrn, pttrns] => pttrns",     Mixfix ("_/ _", [1, 0], 0)),
   220   ("",             "id => aprop",                   Delimfix "_"),
   221   ("",             "longid => aprop",               Delimfix "_"),
   222   ("",             "var => aprop",                  Delimfix "_"),
   223   ("_DDDOT",       "aprop",                         Delimfix "..."),
   224   ("_aprop",       "aprop => prop",                 Delimfix "PROP _"),
   225   ("",             "prop => asms",                  Delimfix "_"),
   226   ("_asms",        "[prop, asms] => asms",          Delimfix "_;/ _"),
   227   ("_bigimpl",     "[asms, prop] => prop",          Mixfix ("((3[| _ |])/ ==> _)", [0, 1], 1)),
   228   ("_ofclass",     "[type, logic] => prop",         Delimfix "(1OFCLASS/(1'(_,/ _')))"),
   229   ("_mk_ofclass",  "_",                             NoSyn),
   230   ("_TYPE",        "type => logic",                 Delimfix "(1TYPE/(1'(_')))"),
   231   ("_K",           "_",                             NoSyn),
   232   ("",             "id => logic",                   Delimfix "_"),
   233   ("",             "longid => logic",               Delimfix "_"),
   234   ("",             "var => logic",                  Delimfix "_"),
   235   ("_DDDOT",       "logic",                         Delimfix "...")];
   236 
   237 val pure_appl_syntax =
   238  [("_appl", "[('b => 'a), args] => logic", Mixfix ("(1_/(1'(_')))", [SynExt.max_pri, 0], SynExt.max_pri)),
   239   ("_appl", "[('b => 'a), args] => aprop", Mixfix ("(1_/(1'(_')))", [SynExt.max_pri, 0], SynExt.max_pri))];
   240 
   241 val pure_applC_syntax =
   242  [("",       "'a => cargs",                  Delimfix "_"),
   243   ("_cargs", "['a, cargs] => cargs",         Mixfix ("_/ _", [SynExt.max_pri, SynExt.max_pri], SynExt.max_pri)),
   244   ("_applC", "[('b => 'a), cargs] => logic", Mixfix ("(1_/ _)", [SynExt.max_pri, SynExt.max_pri], SynExt.max_pri - 1)),
   245   ("_applC", "[('b => 'a), cargs] => aprop", Mixfix ("(1_/ _)", [SynExt.max_pri, SynExt.max_pri], SynExt.max_pri - 1))];
   246 
   247 val pure_sym_syntax =
   248  [("fun",      "[type, type] => type",  Mixfix ("(_/ \\<Rightarrow> _)", [1, 0], 0)),
   249   ("_bracket", "[types, type] => type", Mixfix ("([_]/ \\<Rightarrow> _)", [0, 0], 0)),
   250   ("_ofsort",  "[tid, sort] => type",   Mixfix ("_\\<Colon>_", [SynExt.max_pri, 0], SynExt.max_pri)),
   251   ("_constrain", "['a, type] => 'a",    Mixfix ("_\\<Colon>_", [4, 0], 3)),
   252   ("_idtyp",   "[id, type] => idt",     Mixfix ("_\\<Colon>_", [], 0)),
   253   ("_lambda",  "[pttrns, 'a] => logic", Mixfix ("(3\\<lambda>_./ _)", [0, 3], 3)),
   254   ("==>",      "[prop, prop] => prop",  InfixrName ("\\<Midarrow>\\<Rightarrow>", 1)),
   255   ("_bigimpl", "[asms, prop] => prop",  Mixfix ("((3\\<lbrakk>_\\<rbrakk>)/ \\<Midarrow>\\<Rightarrow> _)", [0, 1], 1)),
   256   ("==",       "['a::{}, 'a] => prop",  InfixrName ("\\<equiv>", 2)),
   257   ("!!",       "[idts, prop] => prop",  Mixfix ("(3\\<And>_./ _)", [0, 0], 0))];
   258 
   259 val pure_xsym_syntax =
   260  [("==>",      "[prop, prop] => prop",  InfixrName ("\\<Longrightarrow>", 1)),
   261   ("=?=",      "['a::{}, 'a] => prop",  InfixrName ("\\<equiv>\\<^sup>?", 2)),
   262   ("_DDDOT",   "aprop",                 Delimfix "\\<dots>"),
   263   ("_bigimpl", "[asms, prop] => prop",  Mixfix ("((3\\<lbrakk>_\\<rbrakk>)/ \\<Longrightarrow> _)", [0, 1], 1)),
   264   ("_DDDOT",   "logic",                 Delimfix "\\<dots>")];
   265 
   266 end;