src/Pure/Syntax/mixfix.ML
changeset 2199 bcb360f80dac
parent 1952 4acc84e5831f
child 2256 e9326ab92fbc
equal deleted inserted replaced
2198:0dddd9575717 2199:bcb360f80dac
     1 (*  Title:      Pure/Syntax/mixfix.ML
     1 (*  Title:      Pure/Syntax/mixfix.ML
     2     ID:         $Id$
     2     ID:         $Id$
     3     Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
     3     Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
     4 
     4 
     5 Mixfix declarations, infixes, binders. Part of the Pure syntax.
     5 Mixfix declarations, infixes, binders. Also parts of the Pure syntax.
     6 *)
     6 *)
     7 
     7 
     8 signature MIXFIX0 =
     8 signature MIXFIX0 =
     9   sig
     9 sig
    10   datatype mixfix =
    10   datatype mixfix =
    11     NoSyn |
    11     NoSyn |
    12     Mixfix of string * int list * int |
    12     Mixfix of string * int list * int |
    13     Delimfix of string |
    13     Delimfix of string |
       
    14     InfixlName of string * int |
       
    15     InfixrName of string * int |
    14     Infixl of int |
    16     Infixl of int |
    15     Infixr of int |
    17     Infixr of int |
    16     Binder of string * int * int
    18     Binder of string * int * int
    17   val max_pri: int
    19   val max_pri: int
    18   end;
    20 end;
    19 
    21 
    20 signature MIXFIX1 =
    22 signature MIXFIX1 =
    21   sig
    23 sig
    22   include MIXFIX0
    24   include MIXFIX0
    23   val type_name: string -> mixfix -> string
    25   val type_name: string -> mixfix -> string
    24   val const_name: string -> mixfix -> string
    26   val const_name: string -> mixfix -> string
    25   val pure_types: (string * int * mixfix) list
    27   val pure_types: (string * int * mixfix) list
    26   val pure_syntax: (string * string * mixfix) list
    28   val pure_syntax: (string * string * mixfix) list
    27   val pure_appl_syntax: (string * string * mixfix) list
    29   val pure_appl_syntax: (string * string * mixfix) list
    28   val pure_applC_syntax: (string * string * mixfix) list
    30   val pure_applC_syntax: (string * string * mixfix) list
    29   end;
    31   val pure_symfont_syntax: (string * string * mixfix) list
       
    32 end;
    30 
    33 
    31 signature MIXFIX =
    34 signature MIXFIX =
    32   sig
    35 sig
    33   include MIXFIX1
    36   include MIXFIX1
    34   val syn_ext_types: string list -> (string * int * mixfix) list -> SynExt.syn_ext
    37   val syn_ext_types: string list -> (string * int * mixfix) list
    35   val syn_ext_consts: string list -> (string * typ * mixfix) list -> SynExt.syn_ext
    38     -> SynExt.syn_ext
    36   end;
    39   val syn_ext_consts: string list -> (string * typ * mixfix) list
       
    40     -> SynExt.syn_ext
       
    41 end;
       
    42 
    37 
    43 
    38 structure Mixfix : MIXFIX =
    44 structure Mixfix : MIXFIX =
    39 struct
    45 struct
    40 
    46 
    41 open Lexicon SynExt Ast SynTrans;
    47 open Lexicon SynExt Ast SynTrans;
       
    48 
    42 
    49 
    43 (** mixfix declarations **)
    50 (** mixfix declarations **)
    44 
    51 
    45 datatype mixfix =
    52 datatype mixfix =
    46   NoSyn |
    53   NoSyn |
    47   Mixfix of string * int list * int |
    54   Mixfix of string * int list * int |
    48   Delimfix of string |
    55   Delimfix of string |
       
    56   InfixlName of string * int |
       
    57   InfixrName of string * int |
    49   Infixl of int |
    58   Infixl of int |
    50   Infixr of int |
    59   Infixr of int |
    51   Binder of string * int * int;
    60   Binder of string * int * int;
    52 
    61 
    53 
    62 
    58   | strip (c :: cs) = c :: strip cs
    67   | strip (c :: cs) = c :: strip cs
    59   | strip [] = [];
    68   | strip [] = [];
    60 
    69 
    61 val strip_esc = implode o strip o explode;
    70 val strip_esc = implode o strip o explode;
    62 
    71 
    63 
    72 fun type_name t (InfixlName _) = t
    64 fun type_name t (Infixl _) = strip_esc t
    73   | type_name t (InfixrName _) = t
       
    74   | type_name t (Infixl _) = strip_esc t
    65   | type_name t (Infixr _) = strip_esc t
    75   | type_name t (Infixr _) = strip_esc t
    66   | type_name t _ = t;
    76   | type_name t _ = t;
    67 
    77 
    68 fun infix_name c = "op " ^ strip_esc c;
    78 fun const_name c (InfixlName _) = c
    69 
    79   | const_name c (InfixrName _) = c
    70 fun const_name c (Infixl _) = infix_name c
    80   | const_name c (Infixl _) = "op " ^ strip_esc c
    71   | const_name c (Infixr _) = infix_name c
    81   | const_name c (Infixr _) = "op " ^ strip_esc c
    72   | const_name c _ = c;
    82   | const_name c _ = c;
    73 
    83 
    74 
    84 
    75 (* syn_ext_types *)
    85 (* syn_ext_types *)
    76 
    86 
    77 fun syn_ext_types logtypes type_decls =
    87 fun syn_ext_types logtypes type_decls =
    78   let
    88   let
    79     fun name_of (t, _, mx) = type_name t mx;
    89     fun name_of (t, _, mx) = type_name t mx;
    80 
    90 
    81     fun mk_infix t p1 p2 p3 =
    91     fun mk_infix sy t p1 p2 p3 =
    82       Mfix ("(_ " ^ t ^ "/ _)", [typeT, typeT] ---> typeT,
    92       Mfix ("(_ " ^ sy ^ "/ _)", [typeT, typeT] ---> typeT, t, [p1, p2], p3);
    83         strip_esc t, [p1, p2], p3);
    93 
    84 
    94     fun mfix_of decl =
    85     fun mfix_of (_, _, NoSyn) = None
    95       let val t = name_of decl in
    86       | mfix_of (t, 2, Infixl p) = Some (mk_infix t p (p + 1) p)
    96         (case decl of
    87       | mfix_of (t, 2, Infixr p) = Some (mk_infix t (p + 1) p p)
    97           (_, _, NoSyn) => None
    88       | mfix_of decl = error ("Bad mixfix declaration for type " ^
    98         | (_, 2, InfixlName (sy, p)) => Some (mk_infix t sy p (p + 1) p)
    89           quote (name_of decl));
    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;
    90 
   104 
    91     val mfix = mapfilter mfix_of type_decls;
   105     val mfix = mapfilter mfix_of type_decls;
    92     val xconsts = map name_of type_decls;
   106     val xconsts = map name_of type_decls;
    93   in
   107   in
    94     syn_ext logtypes mfix xconsts ([], [], [], []) ([], [])
   108     syn_ext logtypes mfix xconsts ([], [], [], []) ([], [])
   107 
   121 
   108     fun binder_typ _ (Type ("fun", [Type ("fun", [_, ty2]), ty3])) =
   122     fun binder_typ _ (Type ("fun", [Type ("fun", [_, ty2]), ty3])) =
   109           [Type ("idts", []), ty2] ---> ty3
   123           [Type ("idts", []), ty2] ---> ty3
   110       | binder_typ c _ = error ("Bad type of binder " ^ quote c);
   124       | binder_typ c _ = error ("Bad type of binder " ^ quote c);
   111 
   125 
   112     fun mfix_of (_, _, NoSyn) = []
   126     fun mfix_of decl =
   113       | mfix_of (c, ty, Mixfix (sy, ps, p)) = [Mfix (sy, ty, c, ps, p)]
   127       let val c = name_of decl in
   114       | mfix_of (c, ty, Delimfix sy) = [Mfix (sy, ty, c, [], max_pri)]
   128         (case decl of
   115       | mfix_of (sy, ty, Infixl p) = mk_infix sy ty (infix_name sy) p (p + 1) p
   129           (_, _, NoSyn) => []
   116       | mfix_of (sy, ty, Infixr p) = mk_infix sy ty (infix_name sy) (p + 1) p p
   130         | (_, ty, Mixfix (sy, ps, p)) => [Mfix (sy, ty, c, ps, p)]
   117       | mfix_of (c, ty, Binder (sy, p, q)) =
   131         | (_, ty, Delimfix sy) => [Mfix (sy, ty, c, [], max_pri)]
   118           [Mfix ("(3" ^ sy ^ "_./ _)", binder_typ c ty, sy, [0, p], q)];
   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;
   119 
   139 
   120     fun binder (c, _, Binder (sy, _, _)) = Some (sy, c)
   140     fun binder (c, _, Binder (sy, _, _)) = Some (sy, c)
   121       | binder _ = None;
   141       | binder _ = None;
   122 
   142 
   123     val mfix = flat (map mfix_of const_decls);
   143     val mfix = flat (map mfix_of const_decls);
   156   ("_asms",     "[prop, asms] => asms",           Delimfix "_;/ _"),
   176   ("_asms",     "[prop, asms] => asms",           Delimfix "_;/ _"),
   157   ("_bigimpl",  "[asms, prop] => prop",           Mixfix ("((3[| _ |])/ ==> _)", [0, 1], 1)),
   177   ("_bigimpl",  "[asms, prop] => prop",           Mixfix ("((3[| _ |])/ ==> _)", [0, 1], 1)),
   158   ("_ofclass",  "[type, logic] => prop",          Delimfix "(1OFCLASS/(1'(_,/ _')))"),
   178   ("_ofclass",  "[type, logic] => prop",          Delimfix "(1OFCLASS/(1'(_,/ _')))"),
   159   ("_K",        "'a",                             NoSyn),
   179   ("_K",        "'a",                             NoSyn),
   160   ("",          "id => logic",                    Delimfix "_"),
   180   ("",          "id => logic",                    Delimfix "_"),
   161   ("",          "var => logic",                   Delimfix "_")]
   181   ("",          "var => logic",                   Delimfix "_")];
   162 
   182 
   163 val pure_appl_syntax =
   183 val pure_appl_syntax =
   164  [("_appl",     "[('b => 'a), args] => logic",    Mixfix ("(1_/(1'(_')))",
   184  [("_appl",     "[('b => 'a), args] => logic",    Mixfix ("(1_/(1'(_')))", [max_pri, 0], max_pri)),
   165                                                     [max_pri, 0], max_pri)),
   185   ("_appl",     "[('b => 'a), args] => aprop",    Mixfix ("(1_/(1'(_')))", [max_pri, 0], max_pri))];
   166   ("_appl",     "[('b => 'a), args] => aprop",    Mixfix ("(1_/(1'(_')))",
       
   167                                                     [max_pri, 0], max_pri))];
       
   168 
   186 
   169 val pure_applC_syntax =
   187 val pure_applC_syntax =
   170  [("",           "'a => cargs",                   Delimfix "_"),
   188  [("",           "'a => cargs",                   Delimfix "_"),
   171   ("_cargs",     "['a, cargs] => cargs",          Mixfix ("_/ _",
   189   ("_cargs",     "['a, cargs] => cargs",          Mixfix ("_/ _", [max_pri, max_pri], max_pri)),
   172                                                    [max_pri, max_pri],
   190   ("_applC",     "[('b => 'a), cargs] => logic",  Mixfix ("(1_/ _)", [max_pri, max_pri], max_pri - 1)),
   173                                                    max_pri)),
   191   ("_applC",     "[('b => 'a), cargs] => aprop",  Mixfix ("(1_/ _)", [max_pri, max_pri], max_pri - 1))];
   174   ("_applC",     "[('b => 'a), cargs] => logic",  Mixfix ("(1_/ _)",
   192 
   175                                                    [max_pri, max_pri],
   193 val pure_symfont_syntax =
   176                                                    max_pri-1)),
   194  [("fun",      "[type, type] => type",            Mixfix ("(_/ \\{Rightarrow} _)", [1, 0], 0)),
   177   ("_applC",     "[('b => 'a), cargs] => aprop",  Mixfix ("(1_/ _)",
   195   ("_bracket", "[types, type] => type",           Mixfix ("([_]/ \\{Rightarrow} _)", [0, 0], 0)),
   178                                                    [max_pri, max_pri],
   196   ("_lambda",  "[idts, 'a] => logic",             Mixfix ("(3\\{lambda}_./ _)", [], 0)),
   179                                                    max_pri-1))];
   197   ("==>",      "[prop, prop] => prop",            InfixrName ("\\{Midarrow}\\{Rightarrow}", 1)),
   180 end;
   198   ("_bigimpl", "[asms, prop] => prop",            Mixfix ("((3\\{ldbrak} _ \\{rdbrak})/ \\{Midarrow}\\{Rightarrow} _)", [0, 1], 1)),
       
   199   ("==",       "['a::{}, 'a] => prop",            InfixrName ("\\{equiv}", 2)),
       
   200   ("!!",       "[idts, prop] => prop",            Mixfix ("(3\\{Vee}_./ _)", [0, 0], 0))];
       
   201 
       
   202 end;