src/Pure/Syntax/mixfix.ML
author paulson
Thu Sep 25 12:09:41 1997 +0200 (1997-09-25)
changeset 3706 e57b5902822f
parent 3690 3f7053bf4237
child 3815 7e8847f8f3a4
permissions -rw-r--r--
Generalized and exported biresolution_from_nets_tac to allow the declaration
of Clarify_tac
     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     InfixlName of string * int |
    15     InfixrName of string * int |
    16     Infixl of int |
    17     Infixr of int |
    18     Binder of string * int * int
    19   val max_pri: int
    20 end;
    21 
    22 signature MIXFIX1 =
    23 sig
    24   include MIXFIX0
    25   val type_name: string -> mixfix -> string
    26   val const_name: string -> mixfix -> string
    27   val pure_types: (string * int * mixfix) list
    28   val pure_syntax: (string * string * mixfix) list
    29   val pure_appl_syntax: (string * string * mixfix) list
    30   val pure_applC_syntax: (string * string * mixfix) list
    31   val pure_sym_syntax: (string * string * mixfix) list
    32 end;
    33 
    34 signature MIXFIX =
    35 sig
    36   include MIXFIX1
    37   val syn_ext_types: string list -> (string * int * mixfix) list
    38     -> SynExt.syn_ext
    39   val syn_ext_consts: string list -> (string * typ * mixfix) list
    40     -> SynExt.syn_ext
    41 end;
    42 
    43 
    44 structure Mixfix : MIXFIX =
    45 struct
    46 
    47 open Lexicon SynExt Ast SynTrans;
    48 
    49 
    50 (** mixfix declarations **)
    51 
    52 datatype mixfix =
    53   NoSyn |
    54   Mixfix of string * int list * int |
    55   Delimfix of string |
    56   InfixlName of string * int |
    57   InfixrName of string * int |
    58   Infixl of int |
    59   Infixr of int |
    60   Binder of string * int * int;
    61 
    62 
    63 (* type / const names *)
    64 
    65 fun strip ("'" :: c :: cs) = c :: strip cs
    66   | strip ["'"] = []
    67   | strip (c :: cs) = c :: strip cs
    68   | strip [] = [];
    69 
    70 val strip_esc = implode o strip o explode;
    71 
    72 fun type_name t (InfixlName _) = t
    73   | type_name t (InfixrName _) = t
    74   | type_name t (Infixl _) = strip_esc t
    75   | type_name t (Infixr _) = strip_esc t
    76   | type_name t _ = t;
    77 
    78 fun const_name c (InfixlName _) = c
    79   | const_name c (InfixrName _) = c
    80   | const_name c (Infixl _) = "op " ^ strip_esc c
    81   | const_name c (Infixr _) = "op " ^ strip_esc c
    82   | const_name c _ = c;
    83 
    84 
    85 (* syn_ext_types *)
    86 
    87 fun syn_ext_types logtypes type_decls =
    88   let
    89     fun name_of (t, _, mx) = type_name t mx;
    90 
    91     fun mk_infix sy t p1 p2 p3 =
    92       Mfix ("(_ " ^ sy ^ "/ _)", [typeT, typeT] ---> typeT, t, [p1, p2], p3);
    93 
    94     fun mfix_of decl =
    95       let val t = name_of decl in
    96         (case decl of
    97           (_, _, NoSyn) => None
    98         | (_, 2, InfixlName (sy, p)) => Some (mk_infix t sy p (p + 1) p)
    99         | (_, 2, InfixrName (sy, p)) => Some (mk_infix t sy (p + 1) p p)
   100         | (sy, 2, Infixl p) => Some (mk_infix t sy p (p + 1) p)
   101         | (sy, 2, Infixr p) => Some (mk_infix t sy (p + 1) p p)
   102         | _ => error ("Bad mixfix declaration for type " ^ quote t))
   103       end;
   104 
   105     val mfix = mapfilter mfix_of type_decls;
   106     val xconsts = map name_of type_decls;
   107   in
   108     syn_ext logtypes mfix xconsts ([], [], [], []) [] ([], [])
   109   end;
   110 
   111 
   112 (* syn_ext_consts *)
   113 
   114 fun syn_ext_consts logtypes const_decls =
   115   let
   116     fun name_of (c, _, mx) = const_name c mx;
   117 
   118     fun mk_infix sy ty c p1 p2 p3 =
   119       [Mfix ("(_ " ^ sy ^ "/ _)", ty, c, [p1, p2], p3),
   120        Mfix ("op " ^ sy, ty, c, [], max_pri)];
   121 
   122     fun binder_typ _ (Type ("fun", [Type ("fun", [_, ty2]), ty3])) =
   123           [Type ("idts", []), ty2] ---> ty3
   124       | binder_typ c _ = error ("Bad type of binder " ^ quote c);
   125 
   126     fun mfix_of decl =
   127       let val c = name_of decl in
   128         (case decl of
   129           (_, _, NoSyn) => []
   130         | (_, ty, Mixfix (sy, ps, p)) => [Mfix (sy, ty, c, ps, p)]
   131         | (_, ty, Delimfix sy) => [Mfix (sy, ty, c, [], max_pri)]
   132         | (_, ty, InfixlName (sy, p)) => mk_infix sy ty c p (p + 1) p
   133         | (_, ty, InfixrName (sy, p)) => mk_infix sy ty c (p + 1) p p
   134         | (sy, ty, Infixl p) => mk_infix sy ty c p (p + 1) p
   135         | (sy, ty, Infixr p) => mk_infix sy ty c (p + 1) p p
   136         | (_, ty, Binder (sy, p, q)) =>
   137             [Mfix ("(3" ^ sy ^ "_./ _)", binder_typ c ty, sy, [0, p], q)])
   138     end;
   139 
   140     fun binder (c, _, Binder (sy, _, _)) = Some (sy, c)
   141       | binder _ = None;
   142 
   143     val mfix = flat (map mfix_of const_decls);
   144     val xconsts = map name_of const_decls;
   145     val binders = mapfilter binder const_decls;
   146     val binder_trs = map mk_binder_tr binders;
   147     val binder_trs' = map (apsnd fix_tr' o mk_binder_tr' o swap) binders;
   148   in
   149     syn_ext logtypes mfix xconsts ([], binder_trs, binder_trs', []) [] ([], [])
   150   end;
   151 
   152 
   153 
   154 (** Pure syntax **)
   155 
   156 val pure_types =
   157   map (fn t => (t, 0, NoSyn))
   158     (terminals @ [logic, "type", "types", "sort", "classes", args, cargs,
   159       "pttrn", "pttrns", "idt", "idts", "aprop", "asms", any, sprop, "dummy"]);
   160 
   161 val pure_syntax =
   162  [("_lambda",      "[pttrns, 'a] => logic",         Mixfix ("(3%_./ _)", [0, 3], 3)),
   163   ("_abs",         "'a",                            NoSyn),
   164   ("",             "'a => " ^ args,                 Delimfix "_"),
   165   ("_args",        "['a, " ^ args ^ "] => " ^ args, Delimfix "_,/ _"),
   166   ("",             "id => idt",                     Delimfix "_"),
   167   ("_idtyp",       "[id, type] => idt",             Mixfix ("_::_", [], 0)),
   168   ("",             "idt => idt",                    Delimfix "'(_')"),
   169   ("",             "idt => idts",                   Delimfix "_"),
   170   ("_idts",        "[idt, idts] => idts",           Mixfix ("_/ _", [1, 0], 0)),
   171   ("",             "idt => pttrn",                  Delimfix "_"),
   172   ("",             "pttrn => pttrns",               Delimfix "_"),
   173   ("_pttrns",      "[pttrn, pttrns] => pttrns",     Mixfix ("_/ _", [1, 0], 0)),
   174   ("",             "id => aprop",                   Delimfix "_"),
   175   ("",             "var => aprop",                  Delimfix "_"),
   176   ("_aprop",       "aprop => prop",                 Delimfix "PROP _"),
   177   ("",             "prop => asms",                  Delimfix "_"),
   178   ("_asms",        "[prop, asms] => asms",          Delimfix "_;/ _"),
   179   ("_bigimpl",     "[asms, prop] => prop",          Mixfix ("((3[| _ |])/ ==> _)", [0, 1], 1)),
   180   ("_ofclass",     "[type, logic] => prop",         Delimfix "(1OFCLASS/(1'(_,/ _')))"),
   181   ("_mk_ofclass",  "_",                             NoSyn),
   182   ("_mk_ofclassS", "_",                             NoSyn),
   183   ("_K",           "_",                             NoSyn),
   184   ("",             "id => logic",                   Delimfix "_"),
   185   ("",             "var => logic",                  Delimfix "_")];
   186 
   187 val pure_appl_syntax =
   188  [("_appl", "[('b => 'a), args] => logic", Mixfix ("(1_/(1'(_')))", [max_pri, 0], max_pri)),
   189   ("_appl", "[('b => 'a), args] => aprop", Mixfix ("(1_/(1'(_')))", [max_pri, 0], max_pri))];
   190 
   191 val pure_applC_syntax =
   192  [("",       "'a => cargs",                  Delimfix "_"),
   193   ("_cargs", "['a, cargs] => cargs",         Mixfix ("_/ _", [max_pri, max_pri], max_pri)),
   194   ("_applC", "[('b => 'a), cargs] => logic", Mixfix ("(1_/ _)", [max_pri, max_pri], max_pri - 1)),
   195   ("_applC", "[('b => 'a), cargs] => aprop", Mixfix ("(1_/ _)", [max_pri, max_pri], max_pri - 1))];
   196 
   197 val pure_sym_syntax =
   198  [("fun",      "[type, type] => type",  Mixfix ("(_/ \\<Rightarrow> _)", [1, 0], 0)),
   199   ("_bracket", "[types, type] => type", Mixfix ("([_]/ \\<Rightarrow> _)", [0, 0], 0)),
   200   ("_ofsort",  "[tid, sort] => type",   Mixfix ("_\\<Colon>_", [max_pri, 0], max_pri)),
   201   ("_constrain", "['a, type] => 'a",    Mixfix ("_\\<Colon>_", [4, 0], 3)),
   202   ("_idtyp",   "[id, type] => idt",     Mixfix ("_\\<Colon>_", [], 0)),
   203   ("_lambda",  "[pttrns, 'a] => logic", Mixfix ("(3\\<lambda>_./ _)", [0, 3], 3)),
   204   ("==>",      "[prop, prop] => prop",  InfixrName ("\\<Midarrow>\\<Rightarrow>", 1)),
   205   ("_bigimpl", "[asms, prop] => prop",  Mixfix ("((3\\<lbrakk>_\\<rbrakk>)/ \\<Midarrow>\\<Rightarrow> _)", [0, 1], 1)),
   206   ("==",       "['a::{}, 'a] => prop",  InfixrName ("\\<equiv>", 2)),
   207   ("!!",       "[idts, prop] => prop",  Mixfix ("(3\\<And>_./ _)", [0, 0], 0))];
   208 
   209 end;